v_thunder has asked for the wisdom of the Perl Monks concerning the following question:
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:
- Can I reset the pointer to the beginning of the string, so that the s/// will match the same text the m// did?
- Are there any pointers to documentation that explains my problem in more detail? I haven't been able to find anything.
- Anyone have any other suggestions?
I await enlightenment :-)
-Dan
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Functions in substitutions (s///) and Perl 5.8.
by kvale (Monsignor) on Oct 23, 2002 at 22:47 UTC | |
Re: Functions in substitutions (s///) and Perl 5.8.
by teichman (Novice) on Oct 24, 2002 at 16:14 UTC | |
by teichman (Novice) on Oct 24, 2002 at 16:44 UTC | |
Re: Functions in substitutions (s///) and Perl 5.8.
by Anonymous Monk on Oct 24, 2002 at 19:35 UTC | |
by teichman (Novice) on Oct 25, 2002 at 15:11 UTC | |
Re: Functions in substitutions (s///) and Perl 5.8.
by mce (Curate) on Oct 25, 2002 at 09:39 UTC | |
Re: Functions in substitutions (s///) and Perl 5.8.
by v_thunder (Scribe) on Oct 25, 2002 at 17:13 UTC | |
by jmarans (Novice) on Nov 01, 2002 at 17:43 UTC |
Back to
Seekers of Perl Wisdom