develooper Front page | perl.perl5.porters | Postings from June 2003

Re: [perl #22354] s/// flags influence regex use in the replacement body

Thread Previous | Thread Next
From:
Rafael Garcia-Suarez
Date:
June 4, 2003 02:39
Subject:
Re: [perl #22354] s/// flags influence regex use in the replacement body
Message ID:
20030603155453.4004f58e.rgarciasuarez@free.fr
"perl-5.8.0@ton.iguana.be (via RT)" wrote:
> 
> This code:
>     perl -wle '
> sub fun {
>     $a = shift;
>     print "fun(<$a>)";
>     print "unexpectdly matched $& at position @-" if /^de/
> }
> $_ = "ab\nde";
> s/^([\w\W]*)/fun("$1")/e'
> 
> outputs (as expected):
> fun(<ab
> de>)
> 
> However, this:
> 
>     perl -wle '
> sub fun {
>     $a = shift;
>     print "fun(<$a>)";
>     print "unexpectdly matched $& at position @-" if /^de/
> }
> $_ = "ab\nde";
> s/^([\w\W]*)/fun("$1")/em'
> 
> outputs:
> 
> fun(<ab
> de>)
> unexpectdly matched de at position 3
> 
> The only difference is the m modifier on the s///
> I didn't expect the "non-reentrant" regex problems here. Until now I
> though the replacement part of a s/// was safe.
> 
> Speculation:
> It seems that m modifier propagates to the regex that's used in fun() which
> gets called from the body.
> Presumably one or more modifiers need localizing.

Yes. Good catch. Setting /m on a match or on a substitution is
equivalent to setting the global special variable $* to 1 (locally).
Thus, it will influence the behaviour of all regexps that haven't /m or
/s set in that scope.

The interaction with $*, and the fact that $* can have remote effects,
makes this bug difficult to solve.

A first step would be to make $* lexically scoped (a compile-time hint,
à la $[). Or to get rid of it.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About