develooper Front page | perl.perl6.language | Postings from August 2001

Re: explicitly declare closures???

Thread Previous | Thread Next
From:
Ken Fox
Date:
August 28, 2001 07:44
Subject:
Re: explicitly declare closures???
Message ID:
3B8BB02F.1F426EC7@vulpes.com
Garrett Goebel wrote:
> From: Dave Mitchell [mailto:davem@fdgroup.co.uk]
> > Okay, to humour me for a mo', what should the following 2 examples
> > output if Perl were doing the "right" thing?
> >
> > sub pr { print $_[0] || 'undef', "\n" }
> >
> > { my $x = 'X'; sub f { $F = sub {pr $x} }}
> > f(); $F->();
> >
> > { my $y = 'Y'; sub g { pr $y; $G = sub {pr $y} }}
> > g(); $G->();
> 
> X
> Y
> Y

Yes, exactly. It would be a violation of the lexical scoping
rules for $x and $y to print anything differently.

> You forgot the other example that someone raised:
> 
> { my $x = 'X'; *h = sub { $H = sub {pr $h} }}
> h(); $H->();
> 
> Which prints:
> 
> Z

Did you mean this?

{ my $z = 'Z'; *h = sub { $H = sub {pr $z} }}
h(); $H->();

Then I agree.

- Ken

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