polettix has asked for the wisdom of the Perl Monks concerning the following question:
I'm bothering you only to ask if this behaviour is what's actually expected (and why, of course):
The fact is that:#!/usr/bin/perl use strict; use warnings; my @lines = split /\n/, <<'END'; begin{verbatim} hello all end{verbatim} END s{((?:begin|end))\{verbatim\}}{$1\{whatever\}} for @lines; print "$_\n" for @lines; __END__ poletti@Polebian:~/sviluppo/perl/pwc/tmp$ perl bug.pl Use of uninitialized value in substitution iterator at bug.pl line 11. Use of uninitialized value in substitution iterator at bug.pl line 11. hello all
is being interpreted as $1{whatever}, i.e. the value corresponding to the whatever key in hash $1, instead of the value of $1, followed by the string {whatever}.$1\{whatever\}
This particular behaviour is a consequence of the unlucky usage of the substitution part delimiters, i.e. {} braces. Should we use another one this would not be triggered, i.e. the following works like a charm (note the square brackets instead of braces in the substitution part):
So, it seems that the escape character \ is being used to allow for embedding braces into braces. But this is not needed at all, as we can see from the following:#!/usr/bin/perl use strict; use warnings; my @lines = split /\n/, <<'END'; begin{verbatim} hello all end{verbatim} END s{((?:begin|end))\{verbatim\}}[$1\{whatever\}] for @lines; print "$_\n" for @lines; __END__ poletti@Polebian:~/sviluppo/perl/pwc/tmp$ perl nobug.pl begin{whatever} hello all end{whatever}
Perl is perfectly happy with the unescaped braces inside the substitution part, even when this very part is delimited by braces.#!/usr/bin/perl use strict; use warnings; my @lines = split /\n/, <<'END'; begin{verbatim} hello all end{verbatim} END my %hash = (whatever => 'world'); s{((?:begin|end))\{verbatim\}}{$hash{whatever}} for @lines; print "$_\n" for @lines; __END__ poletti@Polebian:~/sviluppo/perl/pwc/tmp$ perl bug2.pl world hello all world
Both 5.8.8 and 5.10.0 show the same behaviour. In my view it seems to be a bug; thoughts?
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Io ho capito... ma tu che hai detto?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bug in substitution operator?
by rhesa (Vicar) on Jan 10, 2009 at 20:24 UTC | |
by polettix (Vicar) on Jan 10, 2009 at 20:53 UTC | |
by rhesa (Vicar) on Jan 10, 2009 at 21:09 UTC | |
by polettix (Vicar) on Jan 10, 2009 at 23:10 UTC | |
|
Re: Bug in substitution operator?
by setebos (Beadle) on Jan 10, 2009 at 20:50 UTC | |
by polettix (Vicar) on Jan 10, 2009 at 20:57 UTC | |
|
Re: Bug in substitution operator?
by fullermd (Vicar) on Jan 11, 2009 at 08:22 UTC |