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
    oh, i understood what you mean!

    so thinking about that, seems to me that the solution isn't working at all, unless some effort.

    thanks

    Oha

      What solution isn't working?
        unless the effort of using our (or take care about how the RE is compiled), which is uncommon, the idea of suggesting /(\w)(?(?{$h{$1}})|($!))/$h{$1}/g; isn't working all the time.

        Oha