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 02:56
Subject:
Re: [perl #24250] "return" required in some anonymous closures
Message ID:
p05111b00bbb9608b1bbe@[192.168.56.2]
At 11:44 +0200 10/20/03, Elizabeth Mattijsen wrote:
>At 23:01 +0000 10/19/03, martin@suttles.sca.sfbay.sun.com (via RT) wrote:
>>my $count = 0;
>>my $x = [sub() { $count },
>>	 sub() { return $count },
>>	 sub($) { $count += $_[0] }];
>>$x->[2](3);
>>printf "%d %d\n", $x->[0](), $x->[1]();
>>
>>This prints
>>0 3
>>when it should print
>>3 3

Hmmm... both of the following return correct results also:

BEGIN {my $count = 'foo'}; # $foo properly initialized at compile time
my $x = [sub() { $count },
          sub() { return $count },
          sub($) { $count += $_[0] }];
$x->[2](3);
printf "%d %d\n", $x->[0](), $x->[1]();

my $count = 'foo';
my $x = [sub { $count }, # removed prototype
          sub() { return $count },
          sub($) { $count += $_[0] }];
$x->[2](3);
printf "%d %d\n", $x->[0](), $x->[1]();


So maybe this is an optimizer related problem as well?


Liz



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