Subject: | Using catch(qr// => ...) in POD can be misleading |
In SYNOPSIS, there is a call to catch() with pattern qr//
qr// => sub{ # WILL CATCH ALL EXCEPTIONS
my ($d,$err) = @_;
# end with $d->done/throw/continue/break
},
But this can be misleading because in some cases qr// does not CATCH
ALL EXCEPTIONS.
In perlop (
http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators ),
the empty pattern is decribed as the following.
If the PATTERN evaluates to the empty string, the last
successfully matched regular expression is used instead.
(snip)
If no match has previously succeeded, this will (silently) act
instead as a genuine empty pattern (which will always match).
I think the SYNOPSIS should be modified to avoid using qr//, because
it leads to very inconsistent behavior.
Luckily, though, in the documentation of catch() method it is clealy
stated that "the thrown error can be intercepted (using matching
regexp in catch())", so we don't have to modify this part.
P.S.
I ran the following test and got annoyingly inconsistent behavior!
use strict;
use warnings;
use Async::Defer;
use Test::More tests => 7;
use Test::Exception;
sub createDefer {
my $defer = Async::Defer->new();
$defer->try;
$defer->do(sub {
my $d = shift;
$d->throw('error!');
});
$defer->catch(
qr// => sub {
## Is this supposed to catch ALL exceptions?
my ($d, $e) = @_;
$d->done;
}
);
return $defer;
}
my $defer = createDefer;
lives_ok { $defer->run } "Exception caught";
'foobar' =~ /hoge/;
lives_ok { $defer->run } "Exception caught after 'foobar' =~ /hoge/";
'hoge' =~ /hoge/;
lives_ok { $defer->run } "Exception caught after 'hoge' =~ /hoge/";
'hoge' =~ /error/;
lives_ok { $defer->run } "Exception caught after 'hoge' =~ /error/";
'error' =~ /error/;
lives_ok { $defer->run } "Exception caught after 'error' =~ /error/";
$defer = createDefer;
lives_ok { $defer->run } "Exception caught after the defer is
re-created.";
'hoge' =~ /hoge/;
lives_ok { $defer->run } "Exception caught after the defer is
re-created and 'hoge' =~ /hoge/.";
Result:
1..7
ok 1 - Exception caught
ok 2 - Exception caught after 'foobar' =~ /hoge/
not ok 3 - Exception caught after 'hoge' =~ /hoge/
not ok 4 - Exception caught after 'hoge' =~ /error/
not ok 5 - Exception caught after 'error' =~ /error/
ok 6 - Exception caught after the defer is re-created.
not ok 7 - Exception caught after the defer is re-created and 'hoge'
=~ /hoge/.
perl v5.14.2
Async::Defer 0.9.3