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

Re: [perl #119049] referencing some variables hides others fromPadWalker starting 5.17.2

Thread Previous | Thread Next
From:
Leon Timmermans
Date:
July 31, 2013 23:42
Subject:
Re: [perl #119049] referencing some variables hides others fromPadWalker starting 5.17.2
Message ID:
CAHhgV8jyptMh2SKDhSgpHkhJDhpV5oAiPkytvbQdgbumA1Gkdw@mail.gmail.com
On Thu, Aug 1, 2013 at 1:35 AM, Ricardo SIGNES via RT
<perlbug-followup@perl.org> wrote:
> On Tue Jul 30 16:05:09 2013, sprout wrote:
>> In short: closures close over variables, not lexical scopes, unless
>> string eval is involved.  (So one can work around this by including eval
>> "" in the sub.)
>
> When did this become true in practice?
>
> It does not match up with what I take to be common definition of closures as enclosing a
> complete environment.  I agree that PadWalker is letting Perl programmers go beyond the realm
> of polite behavior, but let's put that aside.
>
> Why shouldn't this program be able to print 2?
>
>   use 5.12.0;
>   use PadWalker 'peek_my';
>
>   my $x = 1;
>
>   use Data::Dumper;
>
>   my $z = sub {
>     warn Dumper(peek_my(1));
>     eval { ${ peek_my(1)->{'$x'} } = 2 }
>   };
>
>   my $w = sub { $z->() };
>
>   $w->();
>
>   say $x;
>
> What are the practical wins to making closure per-variable instead of per-environment?

See also #89544

Leon

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