Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Passing regex result into function

by toolic (Bishop)
on Apr 15, 2016 at 19:23 UTC ( [id://1160575]=note: print w/replies, xml ) Need Help??


in reply to Passing regex result into function

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');

Replies are listed 'Best First'.
Re^2: Passing regex result into function
by choroba (Cardinal) on Apr 17, 2016 at 07:00 UTC
    > 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,

Log In?
Username:
Password:

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

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

    No recent polls found