[opensuse] OT perl regex question
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example: $copy =~ s/[[:graph:]]+//go; thanks Per /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Per Jessen napsal(a):
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go;
thanks Per
/Per Jessen, Zürich
http://stein.cshl.org/genome_informatics/regex/regex4.html part about Variable Interpolation and the "o" Modifier JReidinger -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Mittwoch, 20. August 2008, Per Jessen wrote:
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go;
perldoc perlre doesn't explain it, but perldoc perltut does:
" % cat > simple_replace
#!/usr/bin/perl
$regexp = shift;
$replacement = shift;
while (<>) {
s/$regexp/$replacement/go;
print;
}
^D
% simple_replace regexp regex perlretut.pod
In "simple_replace" we used the "s///g" modifier to replace all occurrences of
the regexp on each line and the "s///o" modifier to compile the regexp only once.
"
So it seems to be just a performance optimization.
HTH
--
Stefan Hundhammer
On Wednesday 20 August 2008 05:52, Stefan Hundhammer wrote:
On Mittwoch, 20. August 2008, Per Jessen wrote:
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go;
...
In "simple_replace" we used the "s///g" modifier to replace all occurrences of the regexp on each line and the "s///o" modifier to compile the regexp only once. "
So it seems to be just a performance optimization.
Hence the flag character 'o'. However, it is not purely an optimization, since it has semantic differences, because it captures interpolated variable values only once, at the time it is compiled.
HTH -- Stefan Hundhammer
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 20 August 2008 14:40:50 Per Jessen wrote:
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go;
It means that the regexp engine will precompile the regular expression, and not recompile it every time the line is executed. Described in "man perlop" Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Wednesday 20 August 2008 14:40:50 Per Jessen wrote:
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go;
It means that the regexp engine will precompile the regular expression, and not recompile it every time the line is executed. Described in "man perlop"
So purely a minor performance optimization? /Per Jessen, Zürich -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wednesday 20 August 2008 15:03:37 Per Jessen wrote:
Anders Johansson wrote:
On Wednesday 20 August 2008 14:40:50 Per Jessen wrote:
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go;
It means that the regexp engine will precompile the regular expression, and not recompile it every time the line is executed. Described in "man perlop"
So purely a minor performance optimization?
Yes. But (from "man perlop"): This avoids expensive run-time recompilations, and is useful when the value you are interpolating won't change over the life of the script. However, mentioning "/o" constitutes a promise that you won't change the variables in the pattern. If you change them, Perl won't even notice. Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Per Jessen wrote:
Anders Johansson wrote:
On Wednesday 20 August 2008 14:40:50 Per Jessen wrote:
Can anyone epxlain to me what the 'o' modifier achieves (or not) in this example:
$copy =~ s/[[:graph:]]+//go; It means that the regexp engine will precompile the regular expression, and not recompile it every time the line is executed. Described in "man perlop"
So purely a minor performance optimization?
In that example I don't think it does anything. The normal use is when there is a variable substitution: while () { /abc$variable.../ } The regex is recompiled on every iteration. /o just compiles it once and that makes a measurable difference if the loop is executed enough times or if the variable is something more complicated. If the variable changes during the iterations, /o also changes the semantics, of course, so it's not always just an optimization. Cheers, Dave -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (6)
-
Anders Johansson
-
Dave Howorth
-
josef reidiner
-
Per Jessen
-
Randall R Schulz
-
Stefan Hundhammer