The stupid question is the question not asked | |
PerlMonks |
Re: Re3: Optimizing a string processing subby MarkM (Curate) |
on Jan 08, 2003 at 22:04 UTC ( [id://225369]=note: print w/replies, xml ) | Need Help?? |
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... :-)
In Section
Seekers of Perl Wisdom
|
|