Dear Monks,

I have some code that does this:

s/\[\[(\w+)\]\]/$helper->($1)/eg;

Where $helper is an anonymous function that returns the text to be substituted. This works great with perl 5.6, but breaks in 5.8 with:

panic: sv_pos_b2u: bad byte offset at [...].

I'm not sure what the problem is, I've also tried:

s/\[\[(\w+)\]\]/@{[$helper->($1)]}/g;

which fails in the same way. I briefly thought about doing something sort of like:

while (m/\[\[(\w+)\]\]/) { my $foo = $helper->($1); s/\[\[(\w+)\]\]/$foo/; }

But the problem with that (besides it being inefficient) is that m// and s/// seem to use the same pointer to mark where in the string the last search ended--so the substitution changes the *next* occurrence of the pattern, not the one the m// matched.

So I'd like to know:

I await enlightenment :-)

-Dan


In reply to Functions in substitutions (s///) and Perl 5.8. by v_thunder

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.