develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Salvaging lexical $_ from deprecation

Thread Previous | Thread Next
From:
Lukas Mai
Date:
March 3, 2013 10:08
Subject:
Re: Salvaging lexical $_ from deprecation
Message ID:
5133211B.2090607@web.de
On 03.03.2013 10:47, Nicholas Clark wrote:
>
> I wasn't aware of the above fun. What strikes me as more fun still is that
> it's radically different output if you don't have a lexical $_ in scope:
>
> $ ./perl -Ilib -wE '$_ = "a"; my @fs = map { sub { $_ } } 1, 2, 3; say $_->() for @fs; say'
> CODE(0x10081bfe8)
> CODE(0x10081bfe8)
> CODE(0x10081bfe8)
> a

Here all subs refer to the same (global) $_, which is later set by 
'for'. So each function effectively prints its own address.

-- 
Lukas Mai <l.mai@web.de>

Thread Previous | Thread Next


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