Skip to content

Commit

Permalink
deprecate smart matching for hints, but silence warnings
Browse files Browse the repository at this point in the history
New versions of perl are deprecating smart match, and they should
eventually be removed. Update the code to silence the warnings related
to smart match to properly disable "deprecated" warnings internally.

Add our own deprecation warnings on all versions of perl that support
smartmatch.

Stop supporting smart match in perl 5.41.
  • Loading branch information
haarg committed Mar 8, 2023
1 parent 2d74f39 commit f56e3fb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
37 changes: 31 additions & 6 deletions lib/Fatal.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ use autodie::Util qw(
on_end_of_compile_scope
);

use constant SMARTMATCH_ALLOWED => ( $] >= 5.010 );
use constant SMARTMATCH_ALLOWED => ( $] >= 5.010 && $] < 5.041 );
use constant SMARTMATCH_CATEGORY => (
!SMARTMATCH_ALLOWED || $] < 5.018 ? undef
: exists $warnings::Offsets{'experimental::smartmatch'} ? 'experimental::smartmatch'
: 'deprecated'
);

use constant LEXICAL_TAG => q{:lexical};
use constant VOID_TAG => q{:void};
Expand Down Expand Up @@ -48,7 +53,9 @@ use constant ERROR_AUTODIE_CONFLICT => q{"no autodie '%s'" is not allowed while

use constant ERROR_FATAL_CONFLICT => q{"use Fatal '%s'" is not allowed while "no autodie '%s'" is in effect};

use constant ERROR_58_HINTS => q{Non-subroutine %s hints for %s are not supported under Perl 5.8.x};
use constant ERROR_SMARTMATCH_HINTS => q{%s hints for %s must be code, regexp, or undef. Use of other values is deprecated and only supported on Perl 5.10 through 5.40.};

use constant WARNING_SMARTMATCH_DEPRECATED => q{%s hints for %s must be code, regexp, or undef. Use of other values is deprecated and will be removed before Perl 5.42.};

# Older versions of IPC::System::Simple don't support all the
# features we need.
Expand Down Expand Up @@ -1089,8 +1096,6 @@ sub _one_invocation {

my $code = qq[
no warnings qw(unopened uninitialized numeric);
no if \$\] >= 5.017011, warnings => "experimental::smartmatch";
no if \$warnings::Offsets{"deprecated::smartmatch"}, warnings => "deprecated";
if (wantarray) {
my \@results = $call(@argv);
Expand All @@ -1111,11 +1116,21 @@ sub _one_invocation {

$match = q[ $hints->{list}->(@results) ];
}
elsif ( ref($hints->{list}) eq 'Regexp' ) {
$match = q[ grep $_ =~ $hints->{list}, @results ];
}
elsif ( !defined $hints->{list} ) {
$match = q[ grep !defined, @results ];
}
elsif ( SMARTMATCH_ALLOWED ) {
$match = q[ @results ~~ $hints->{list} ];
warnings::warnif('deprecated', sprintf(WARNING_SMARTMATCH_DEPRECATED, 'list', $sub));
if (SMARTMATCH_CATEGORY) {
$match = sprintf q[ do { no warnings '%s'; %s } ], SMARTMATCH_CATEGORY, $match;
}
}
else {
croak sprintf(ERROR_58_HINTS, 'list', $sub);
croak sprintf(ERROR_SMARTMATCH_HINTS, 'list', $sub);
}

$code .= qq{
Expand Down Expand Up @@ -1156,11 +1171,21 @@ sub _one_invocation {
# works in 5.8.x, and always works in 5.10.1
$match = q[ $hints->{scalar}->($retval) ];
}
elsif ( ref($hints->{scalar}) eq 'Regexp' ) {
$match = q[ $retval =~ $hints->{scalar} ];
}
elsif ( !defined $hints->{scalar} ) {
$match = q[ !defined $retval ];
}
elsif (SMARTMATCH_ALLOWED) {
$match = q[ $retval ~~ $hints->{scalar} ];
warnings::warnif('deprecated', sprintf(WARNING_SMARTMATCH_DEPRECATED, 'scalar', $sub));
if (SMARTMATCH_CATEGORY) {
$match = sprintf q[ do { no warnings '%s'; %s } ], SMARTMATCH_CATEGORY, $match;
}
}
else {
croak sprintf(ERROR_58_HINTS, 'scalar', $sub);
croak sprintf(ERROR_SMARTMATCH_HINTS, 'scalar', $sub);
}

return $code . qq{
Expand Down
4 changes: 2 additions & 2 deletions lib/autodie/exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ our $DEBUG = 0;
use overload
q{""} => "stringify",
# Overload smart-match only if we're using 5.10 or up
($] >= 5.010 ? ('~~' => "matches") : ()),
fallback => 1
(( $] >= 5.010 && $] < 5.041 ) ? ('~~' => "matches") : ()),
fallback => 1,
;

my $PACKAGE = __PACKAGE__; # Useful to have a scalar for hash keys.
Expand Down
9 changes: 5 additions & 4 deletions lib/autodie/hints.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ return value of an autodying subroutine. If the match returns true,
C<autodie> considers the subroutine to have failed.
If the hint provided is a subroutine, then C<autodie> will pass
the complete return value to that subroutine. If the hint is
any other value, then C<autodie> will smart-match against the
value provided. In Perl 5.8.x there is no smart-match operator, and as such
only subroutine hints are supported in these versions.
the complete return value to that subroutine. If the hint is a regexp object,
then C<autodie> will match it against the return value. If the hint is undef,
the return value must be undef. On Perl versions 5.10 and newer, any other
value can be provided and it will be smart matched against the value provided.
However, smart matched values like this are deprecated.
Hints can be provided for both scalar and list contexts. Note
that an autodying subroutine will never see a void context, as
Expand Down
9 changes: 4 additions & 5 deletions t/exceptions-smartmatch.t
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/perl -w
use strict;
use Test::More;
use Fatal ();

BEGIN { plan skip_all => "Perl 5.10 only tests" if $] < 5.010; }
BEGIN { plan skip_all => "requires perl with smartmatch support" unless Fatal::SMARTMATCH_ALLOWED; }

# These are tests that depend upon 5.10 (eg, smart-match).
# These are tests that depend upon smartmatch.
# Basic tests should go in basic_exceptions.t

use 5.010;
use warnings ();
use constant NO_SUCH_FILE => 'this_file_had_better_not_exist_xyzzy';
no if $] >= 5.017011, warnings => "experimental::smartmatch";
no if exists $warnings::Offsets{"deprecated::smartmatch"},
warnings => "deprecated";
no if Fatal::SMARTMATCH_CATEGORY, 'warnings', Fatal::SMARTMATCH_CATEGORY;

plan 'no_plan';

Expand Down

0 comments on commit f56e3fb

Please sign in to comment.