summaryrefslogtreecommitdiff
path: root/cpan/autodie
diff options
context:
space:
mode:
authorZefram <zefram@fysh.org>2017-11-22 17:23:57 +0000
committerZefram <zefram@fysh.org>2017-11-22 17:23:57 +0000
commit5f3202fa3e77c4a20de590df045af4683aaedffa (patch)
tree5572a671df6c4586c7d77cf8f98787eb0f457f07 /cpan/autodie
parent5e1cca32ac612f0b59508a99fbff671a693f39b9 (diff)
downloadperl-5f3202fa3e77c4a20de590df045af4683aaedffa.tar.gz
eviscerate smartmatch
Regularise smartmatch's operand handling, by removing the implicit enreferencement and just supplying scalar context. Eviscerate its runtime behaviour, by removing all the matching rules other than rhs overloading. Overload smartmatching in the Regexp package to perform regexp matching. There are consequential customisations to autodie, in two areas. Firstly, autodie::exception objects are matchers, but autodie has been advising smartmatching with the exception on the lhs. This has to change to the rhs, in both documentation and tests. Secondly, it uses smartmatching as part of its hint mechanism. Most of the hint examples, in documentation and tests, have to change to subroutines, to be portable across Perl versions.
Diffstat (limited to 'cpan/autodie')
-rw-r--r--cpan/autodie/lib/autodie/exception.pm19
-rw-r--r--cpan/autodie/lib/autodie/hints.pm19
-rw-r--r--cpan/autodie/t/exceptions.t16
-rw-r--r--cpan/autodie/t/lib/Hints_pod_examples.pm16
4 files changed, 40 insertions, 30 deletions
diff --git a/cpan/autodie/lib/autodie/exception.pm b/cpan/autodie/lib/autodie/exception.pm
index 73058085e0..b3fcff9bb4 100644
--- a/cpan/autodie/lib/autodie/exception.pm
+++ b/cpan/autodie/lib/autodie/exception.pm
@@ -4,7 +4,7 @@ use strict;
use warnings;
use Carp qw(croak);
-our $VERSION = '2.29'; # VERSION: Generated by DZP::OurPkg:Version
+our $VERSION = '2.29001';
# ABSTRACT: Exceptions from autodying functions.
our $DEBUG = 0;
@@ -195,12 +195,10 @@ sub eval_error { return $_[0]->{$PACKAGE}{eval_error}; }
if ( $e->matches('open') ) { ... }
- if ( $e ~~ 'open' ) { ... }
+ if ( 'open' ~~ $e ) { ... }
C<matches> is used to determine whether a
-given exception matches a particular role. On Perl 5.10,
-using smart-match (C<~~>) with an C<autodie::exception> object
-will use C<matches> underneath.
+given exception matches a particular role.
An exception is considered to match a string if:
@@ -221,6 +219,17 @@ C<CORE::open> subroutine does C<:file>, C<:io> and C<:all>.
See L<autodie/CATEGORIES> for further information.
+On Perl 5.10 and above, using smart-match (C<~~>) with an
+C<autodie::exception> object will use C<matches> underneath. This module
+used to recommend using smart-match with the exception object on the left
+hand side, but in newer Perls that no longer works. The smart-match
+facility of this class can now only be used with the exception object
+on the right hand side. Having the exception object on the right also
+works on older Perls, back to 5.10. Beware that this facility can only
+be relied upon when it is certain that the exception object actually is
+an C<autodie::exception> object; it is no more capable than an explicit
+call to the C<matches> method.
+
=back
=cut
diff --git a/cpan/autodie/lib/autodie/hints.pm b/cpan/autodie/lib/autodie/hints.pm
index beaefcc28a..be9fbceb47 100644
--- a/cpan/autodie/lib/autodie/hints.pm
+++ b/cpan/autodie/lib/autodie/hints.pm
@@ -5,7 +5,7 @@ use warnings;
use constant PERL58 => ( $] < 5.009 );
-our $VERSION = '2.29'; # VERSION: Generated by DZP::OurPkg:Version
+our $VERSION = '2.29001';
# ABSTRACT: Provide hints about user subroutines to autodie
@@ -115,8 +115,9 @@ has been checked.
=head2 Example hints
-Hints may consist of scalars, array references, regular expressions and
-subroutine references. You can specify different hints for how
+Hints may consist of subroutine references, objects overloading
+smart-match, regular expressions, and depending on Perl version possibly
+other things. You can specify different hints for how
failure should be identified in scalar and list contexts.
These examples apply for use in the C<AUTODIE_HINTS> subroutine and when
@@ -125,16 +126,16 @@ calling C<autodie::hints->set_hints_for()>.
The most common context-specific hints are:
# Scalar failures always return undef:
- { scalar => undef }
+ { scalar => sub { !defined($_[0]) } }
# Scalar failures return any false value [default expectation]:
{ scalar => sub { ! $_[0] } }
# Scalar failures always return zero explicitly:
- { scalar => '0' }
+ { scalar => sub { defined($_[0]) && $_[0] eq '0' } }
# List failures always return an empty list:
- { list => [] }
+ { list => sub { !@_ } }
# List failures return () or (undef) [default expectation]:
{ list => sub { ! @_ || @_ == 1 && !defined $_[0] } }
@@ -151,7 +152,7 @@ The most common context-specific hints are:
\&foo,
{
scalar => qr/^ _? FAIL $/xms,
- list => [-1],
+ list => sub { @_ == 1 && $_[0] eq -1 },
}
);
@@ -159,8 +160,8 @@ The most common context-specific hints are:
autodie::hints->set_hints_for(
\&foo,
{
- scalar => 0,
- list => [0],
+ scalar => sub { defined($_[0]) && $_[0] == 0 },
+ list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 },
}
);
diff --git a/cpan/autodie/t/exceptions.t b/cpan/autodie/t/exceptions.t
index 4e7545d1ee..ab6f07de44 100644
--- a/cpan/autodie/t/exceptions.t
+++ b/cpan/autodie/t/exceptions.t
@@ -19,10 +19,10 @@ eval {
};
ok($@, "Exception thrown" );
-ok($@ ~~ 'open', "Exception from open" );
-ok($@ ~~ ':file', "Exception from open / class :file" );
-ok($@ ~~ ':io', "Exception from open / class :io" );
-ok($@ ~~ ':all', "Exception from open / class :all" );
+ok('open' ~~ $@, "Exception from open" );
+ok(':file' ~~ $@, "Exception from open / class :file" );
+ok(':io' ~~ $@, "Exception from open / class :io" );
+ok(':all' ~~ $@, "Exception from open / class :all" );
eval {
no warnings 'once'; # To prevent the following close from complaining.
@@ -39,10 +39,10 @@ eval {
like($@, qr{Can't close filehandle 'THIS_FILEHANDLE_AINT_OPEN'},"Nice msg from close");
ok($@, "Exception thrown" );
-ok($@ ~~ 'close', "Exception from close" );
-ok($@ ~~ ':file', "Exception from close / class :file" );
-ok($@ ~~ ':io', "Exception from close / class :io" );
-ok($@ ~~ ':all', "Exception from close / class :all" );
+ok('close' ~~ $@, "Exception from close" );
+ok(':file' ~~ $@, "Exception from close / class :file" );
+ok(':io' ~~ $@, "Exception from close / class :io" );
+ok(':all' ~~ $@, "Exception from close / class :all" );
ok $@ eq $@.'', "string overloading is complete (eq)";
ok( ($@ cmp $@.'') == 0, "string overloading is complete (cmp)" );
diff --git a/cpan/autodie/t/lib/Hints_pod_examples.pm b/cpan/autodie/t/lib/Hints_pod_examples.pm
index 05db908e18..72a58a5ce5 100644
--- a/cpan/autodie/t/lib/Hints_pod_examples.pm
+++ b/cpan/autodie/t/lib/Hints_pod_examples.pm
@@ -17,17 +17,17 @@ use autodie::hints;
sub AUTODIE_HINTS {
return {
# Scalar failures always return undef:
- undef_scalar => { fail => undef },
+ undef_scalar => { fail => sub { !defined($_[0]) } },
# Scalar failures return any false value [default behaviour]:
false_scalar => { fail => sub { return ! $_[0] } },
# Scalar failures always return zero explicitly:
- zero_scalar => { fail => '0' },
+ zero_scalar => { fail => sub { defined($_[0]) && $_[0] eq '0' } },
# List failures always return empty list:
# We never want these called in a scalar context
- empty_list => { scalar => sub { 1 }, list => [] },
+ empty_list => { scalar => sub { 1 }, list => sub { !@_ } },
# List failures return C<()> or C<(undef)> [default expectation]:
default_list => { fail => sub { ! @_ || @_ == 1 && !defined $_[0] } },
@@ -54,8 +54,8 @@ sub undef_n_error_list { return wantarray ? @_ : $_[0] }
autodie::hints->set_hints_for(
\&foo,
{
- scalar => 0,
- list => [0],
+ scalar => sub { defined($_[0]) && $_[0] == 0 },
+ list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 },
}
);
@@ -67,7 +67,7 @@ autodie::hints->set_hints_for(
\&re_fail,
{
scalar => qr/^ _? FAIL $/xms,
- list => [-1],
+ list => sub { @_ == 1 && $_[0] eq -1 },
}
);
@@ -77,8 +77,8 @@ sub re_fail { return wantarray ? @_ : $_[0] }
autodie::hints->set_hints_for(
\&bar,
{
- scalar => 0,
- list => [0],
+ scalar => sub { defined($_[0]) && $_[0] == 0 },
+ list => sub { @_ == 1 && defined($_[0]) && $_[0] == 0 },
}
);