Interesting. It does sound buggy.

And it does the same thing with brackets too, notice. Simpler example:

@s = ("foo bar baz") x 4; $bar = "rab"; %bar = (1 => "eek"); @bar = (undef, 'oof'); $s[0] =~ s{(bar)} {$bar\{1\}}; $s[1] =~ s{(bar)} [$bar\{1\}]; $s[2] =~ s{(bar)} {$bar\[1\]}; $s[3] =~ s{(bar)} [$bar\[1\]]; for $i (0..3) { print "$i -> '$s[$i]'\n"; }

Yields

0 -> 'foo eek baz' 1 -> 'foo rab{1} baz' 2 -> 'foo rab[1] baz' 3 -> 'foo oof baz'

(5.8 here)

So something about having the delimiter be the same as your subscripting character makes it real hard to escape.

For extra fun, notice that using two \'s in the sub fails as "replacement not terminated", and using 3 leaves one sitting in your string:

4 -> 'foo rab\[1] baz'

So it doesn't seem like you can all that easily work around it except by changing the delimiters. Maybe playing with escaping... yes, that seems to work:

$s[4] =~ s{(bar)} [$bar\E\[1\]]; 4 -> 'foo rab[1] baz'

Wackiness.


In reply to Re: Bug in substitution operator? by fullermd
in thread 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.