in reply to Using $1,$2 etc in closures..

I think the key point here is that $1, $2, $3 etc are global variables. Referring to a global from within an anonymous sub doesn't make it a closure.

The following code generates a closure since $x is a lexical in the scope enclosing the anonymous sub:

sub make_closure { my $x = 1; my $closure = sub { return $x++; }; return $closure; } my $cls = make_closure(); print &$cls; print &$cls; print &$cls;

Replies are listed 'Best First'.
Re: Re: Using $1,$2 etc in closures..
by Elian (Parson) on Mar 07, 2003 at 22:23 UTC
    Regex match variables aren't actually global. They're... odd. But not global. (Neither are they lexical, though they're mostly lexical. Mostly)

      Oh, well that clears that up then. Thanks :-)

      Whatever they are, I think we can all probably agree they're fairly transient in nature and if they contain anything you want then you ought to be saving it somewhere safe real quick.