Hi all,

I'm bothering you only to ask if this behaviour is what's actually expected (and why, of course):

#!/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
The fact is that:
$1\{whatever\}
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}.

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

#!/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}
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 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
Perl is perfectly happy with the unescaped braces inside the substitution part, even when this very part is delimited by braces.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.