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... :-)
|
|---|