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

(?{ ... }) captured the original $ok in a closure so when the code executed the second time the my() caused the returned $ok to be a different $ok than the one the (?{...}) block was changing. Consider writing to a global instead.
  • Comment on Re: Variable scoping oddity inside (??{ ... })

Replies are listed 'Best First'.
Re: Re: Variable scoping oddity inside (??{ ... })
by Roger (Parson) on Oct 31, 2003 at 04:48 UTC
      bleadperl properly gives the 'Variable "$ok" will not stay shared' warning
Re: Re: Variable scoping oddity inside (??{ ... })
by pg (Canon) on Oct 31, 2003 at 05:44 UTC

    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.

      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 ]})/