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 | |
by grantm (Parson) on Mar 08, 2003 at 01:25 UTC |