Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re3: Optimizing a string processing sub

by MarkM (Curate)
on Jan 08, 2003 at 22:04 UTC ( [id://225369]=note: print w/replies, xml ) Need Help??


in reply to Re3: Optimizing a string processing sub
in thread Optimizing a string processing sub

To ensure that characters such as '^', '-' or ']' do not interfere, I would always be tempted to surround the interpolated value with \Q..\E.

An alternative (more intuitive?) method of forcing list context on an expression, is to assign the expression to a list:

my $count = () = $x =~ /[\Q$y\E]/g;

The comma operator in scalar context returns the second argument. The intermediate assignment above causes the comma operator to believe it is an list context.

UPDATE: chromatic beat me to the draw by 2 minutes on the ()= trick. However -- chromatic: you cannot claim credit for being the first to use this trick (chromatic's context chaining). I remember it from the time that it was being argued over on the perl5-porters mailing list, perhaps 5 years ago... :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-03-28 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found