sdsommer has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to pass the result of a regex into a function, as the first of three parameters. For example:
my $str = 'abc'; PassRegex($str =~ /b/i, 'parm2', 'parm3');
The subroutine looks like this:
use Data::Dumper; sub PassRegex { print Dumper(\@_); print "BoolExpr = $_[0]\n"; print "parm2 = $_[1]\n"; print "parm3 = $_[2]\n"; return; }
This works fine when the regex evaluates to true (1); if I look at @_ inside the function, I see 1 as the 0th element. However, if I change the regex to /x/i, so that it evaluates to false (the empty string), then the 0th element of @_ seems to disappear. Data:Dumper shows a 0th element, but it is empty -- completely empty, not the empty string -- and $_[0] returns the second parameter. Contrast this case to the case where I explicitly pass in the empty string to the function, and @_ contains what one would expect.
My question is, what do regexes that don't produce a match actually return, if not the empty string? Also, I realize that there are probably other ways to accomplish what I'm trying to do -- for example, I could quote the regex so that it would be evaluated inside the function -- but I'm curious about why this way doesn't work.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing regex result into function
by toolic (Bishop) on Apr 15, 2016 at 19:23 UTC | |
by choroba (Cardinal) on Apr 17, 2016 at 07:00 UTC | |
|
Re: Passing regex result into function
by nysus (Parson) on Apr 15, 2016 at 19:17 UTC | |
by AnomalousMonk (Archbishop) on Apr 16, 2016 at 00:24 UTC | |
by sdsommer (Initiate) on Apr 15, 2016 at 20:49 UTC | |
|
Re: Passing regex result into function
by nysus (Parson) on Apr 15, 2016 at 19:32 UTC |