in reply to Re: Variable scoping oddity inside (??{ ... })
in thread Variable scoping oddity inside (??{ ... })

To be more precise, Perl actually used the same instance of closure in this case. If Perl creates a new instance of the closure every time it enters the regexp, then this behavior will disappear.

  • Comment on Re: Re: Variable scoping oddity inside (??{ ... })

Replies are listed 'Best First'.
Re: Re: Re: Variable scoping oddity inside (??{ ... })
by diotalevi (Canon) on Oct 31, 2003 at 15:06 UTC

    Ok, yes. That's because the m(...)'s (?{...}) is part of the optree and isn't being renewed each time. Using qr() wouldn't help because that'd have the same problem except now the closure would be bound to the qr() object.

    An alternative idea interpolates a random value into a (?#...) comment node so every time the code executes the regex's string value is different and the regex is recompiled (of course, don't use /o on this).

    /...(?{ ... })(?#@{[ rand ]})/