develooper Front page | perl.perl5.porters | Postings from October 2003

Re: [perl #24250] "return" required in some anonymous closures

From:
Elizabeth Mattijsen
Date:
October 20, 2003 06:06
Subject:
Re: [perl #24250] "return" required in some anonymous closures
Message ID:
p05111b07bbb98ddfbbd4@[192.168.56.2]
At 08:58 -0400 10/20/03, Mark Jason Dominus wrote:
>Dave Mitchell <davem@fdgroup.com>:
>>  This isn't a bug, it's a feature :-)
>>
>>  sub () {$lexical} is a constant sub generator, ie each call to 'sub'
>>  returns a new constant sub rather than a closure.
>
>Oh well, so much for the principle of least astonishment.
>
>What's the workaround for this feature?

Dropping the prototype seems to work:

my $foo = 'foo';
my $get = sub {$foo}; # look, no prototype!
my $set = sub ($) {$foo = $_[0]};
$set->('bar');
print "foo = $foo = ".$get->()."\n";
__END__
foo = bar = bar


Liz



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About