Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: Tokenizing and qr// <=> /g interplay

by Tanktalus (Canon)
on Apr 23, 2005 at 19:22 UTC ( [id://450774]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Tokenizing and qr// <=> /g interplay
in thread Tokenizing and qr// <=> /g interplay

When Perl sees a regex comprised solely of a single variable, Perl checks to see if that variable is a Regexp object (what qr// returns). If it is, Perl knows that the regex has already been compiled, so it simply uses the compiled regex in the place of the regex.

Really? That is very interesting. So if I have:

my $true = qr/y(?:es|up)?|1|enabled?/i; my $false = qr/n(?:o(?:pe)?)?|0|disabled?/i; die "Need boolean input" unless /^(?:$true|$false)$/; if (/$true/) { do_stuff(); }
you're saying that in the die's unless clause, it will need to completely recompile the regex? That is not my interpretation, but I could be completely wrong here.

My assumption is that both $true and $false are compiled once, and only once, and the unless modifier above would not need to recompile either one.

Even if that is the case, I use code like the above because I like to be able to reuse a common criteria for truth and falseness across many expressions - sometimes, as in the die statement above, for validation that the value is something (i.e., not a typo - if someone had "y]", we'd not accidentally treat that as a false value, we'd simply reject it so the user could fix the typo), or, at other times, such as the if statement above, just to see which one it was. Which goes to the OP's question on why it's useful, somewhat in agreement with other posts here. I'm just showing a concrete example of real, live, production code where I use this construct.

Replies are listed 'Best First'.
Re^5: Tokenizing and qr// <=> /g interplay
by japhy (Canon) on Apr 23, 2005 at 19:25 UTC
    It won't need to recompile the entire regex each time, because $true and $false haven't changed, but Perl will need to compare the string representation of the regex with the representation of it the last time it was at this opcode. And the first time through, yes, it will compile the regex.
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2025-06-19 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.