in reply to Re^2: regex s/// using hash
in thread regex s/// using hash
i'm not using (?(cond)|..) but (? (?{code}) | (?!) )
Adding whitespace doesn't change what it is.
i'm also a bit confused about the closures: being used in scope those regexp aren't returned so i can't see side-effect of using our or my, what am i missing
A closure occurs when a sub persists longer than the scope in which it was created. The code in (?{code}), (??{code}) and (?(?{code})|..) is compiled into a sub. The regex persists beyond the scope in which it is created (to avoid needless recompiling of the regex), causing a closure.
sub func_lex { my ($var) = @_; '' =~ /(?{ print("$var\n"); })/; } sub func_pkg { our ($var) = @_; '' =~ /(?{ print("$var\n"); })/; } func_lex('foo'); # foo func_lex('bar'); # foo !!! func_pkg('foo'); # foo func_pkg('bar'); # bar
Update: Added func_pkg to example.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: regex s/// using hash
by oha (Friar) on Oct 16, 2007 at 14:01 UTC | |
by ikegami (Patriarch) on Oct 16, 2007 at 14:06 UTC | |
by oha (Friar) on Oct 16, 2007 at 14:12 UTC |