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?In reply to Bug in substitution operator? by polettix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |