in reply to A Real Closure
in thread Unusual Closure Behaviour
So I tried it. It gives me a warning that "$x will not stay shared", but the result seems to work! That is, a created named function seems to reference the same variable as a standard closure created in the same scope, and running the creator again (which makes a different local $x) keeps distinct identities.
So what's going on here? Are the docs outdated? Is this working by accident or happenstance? Does the presence of a regular closure somehow make it work?
—John
use v5.6.1; # Active State build 626 use strict; use warnings; sub outer { my $x= shift; my $name= shift; my $closure= sub { return $x++; }; eval "sub $name { return \$x++; }"; return $closure; } my $r1= outer (1, 'f1'); my $r2= \&f1; print $r1->(), $r2->(), "\n"; my $r3= outer ('A', 'f2'); my $r4= \&f2; print $r3->(), $r4->(),$r3->(), $r4->(), "\n"; print $r1->(), $r2->(),$r1->(), $r2->(), "\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: A Real Closure
by japhy (Canon) on Jul 12, 2001 at 18:41 UTC | |
by John M. Dlugosz (Monsignor) on Jul 12, 2001 at 18:50 UTC | |
by tilly (Archbishop) on Jul 14, 2001 at 22:01 UTC |