What happens to a non-matching regex in a Perl subroutine call? -
i'm trying make sense of what's happening non-matching regex in subroutine call. consider script:
sub routine{ print dumper(\@_); } $s = 'abc123'; # these pass single element &routine &routine( $s =~ /c/ ); # 1. passes (1) &routine(2 == 3); # 2. passes ('') &routine(3 == 3); # 3. passes (1) # following 2 calls appear identical &routine( $s =~ /foobar/ ); # 4. passes () &routine(); # 5. passes ()
in above script, numbers 1, 2 , 3 pass single value &routine
. i'm surprised number 4 doesn't pass false value, rather passes nothing @ all!
it doesn't seem possible non-matching regex evaluates nothing @ all, since same sort of signature in conditional isn't valid:
# fine if( $s =~ /foobar/ ){ print "it's true!\n"; } # syntax error if( ){ print "hmm...\n"; # :/ }
what happens non-matching regex when it's used in subroutine call? further, possible &routine
figure out whether or not it's been called non-matching regex, vs nothing @ all?
when match operator =~
used in list context returns list of matches. when there no matches list empty (also called empty list), , empty list passed sub routine
in turn causes @_
empty.
if explicitly want pass false value of "did expression return matches?" need perform match in scalar context. can using scalar
keyword
&routine( scalar $s =~ /foobar/ );
which pass value ''
(false) routine
sub. calling sub without arguments passes empty list, final example correctly written:
if ( () ) { print "hmm...\n"; }
which not syntax error because in perl 0
, ''
, , ()
represent false.
Comments
Post a Comment