Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Passing regex result into function

by sdsommer (Initiate)
on Apr 15, 2016 at 18:56 UTC ( [id://1160565]=perlquestion: print w/replies, xml ) Need Help??

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
    PassRegex($str =~ /b/i, 'parm2', 'parm3');
    I believe that regex is evaluated in list context, not scalar context. In that case, the m operator returns an empty list when there is no match. From perlop:
    an empty list is returned upon failure.
    An empty list is a list with no items. So, when there is no match, you actually pass a list of 2 items to your sub, not 3. You could use this to force scalar context:
    PassRegex(0+($str =~ /x/i), 'parm2', 'parm3');
    Or, be more explicit with:
    PassRegex($str =~ /x/i ? 1 : 0, 'parm2', 'parm3');
      > force scalar context

      I'd use scalar for that:

      scalar($str =~ /x/i)

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Passing regex result into function
by nysus (Parson) on Apr 15, 2016 at 19:17 UTC
    Try:
    my $str = 'abc'; my ($arg) = $str =~ /b/i; PassRegex($arg, 'parm2', 'parm3');

    Regex matches behave differently depending if they are in a list or scalar context.

    Update: And to more specifically answer your question, no match returns undef

    .

    Update 2: You could also do something like this:

    PassRegex($str =~ /b/i || 0, 'parm2', 'parm3');

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      The  m// operator returns false (the empty string), not undef, on match failure when evaluated in scalar context:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $str = 'abc'; PassRegex(scalar($str =~ /x/i), 'parm2', 'parm3'); ;; use Data::Dumper; ;; sub PassRegex { print Dumper(\@_); print qq{BoolExpr = '$_[0]' parm2 = '$_[1]' parm3 = '$_[2]'}; return; } " $VAR1 = [ '', 'parm2', 'parm3' ]; BoolExpr = '' parm2 = 'parm2' parm3 = 'parm3'
      Please see Regexp Quote-Like Operators.


      Give a man a fish:  <%-{-{-{-<

      Thank You! That answers my question. Next time I'll remember to check the documentation first.

Re: Passing regex result into function
by nysus (Parson) on Apr 15, 2016 at 19:32 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1160565]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-03-28 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found