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

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

From:
Nick Ing-Simmons
Date:
October 20, 2003 05:32
Subject:
Re: [perl #24250] "return" required in some anonymous closures
Message ID:
20031020123211.2513.15@llama.elixent.com
Elizabeth Mattijsen <liz@dijkmat.nl> writes:
>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

No. That has initialized a $count lexical to the BEGIN

>my $x = [sub() { $count },

No lexical $count in scope here 'tis a global.

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




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