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

Re: explicitly declare closures???

Thread Previous | Thread Next
From:
Piers Cawley
Date:
August 21, 2001 09:33
Subject:
Re: explicitly declare closures???
Message ID:
m3vgjhsgxv.fsf@iest.bofh.org.uk
Dave Mitchell <davem@fdgroup.co.uk> writes:

> Just thought I'd run the following up the flagpole to see if anyone
> laughs at it....
> 
> Closures are useful, powerful things, but they can also be
> dangerous and counter-intuitive, espcially to the uninitiated. For example,
> how many people could say what the following should output,
> with and without $x commented out, and why:
> 
> {
>     my $x = "bar";
>     sub foo {
> 	# $x  # <- uncommenting this line changes the outcome
> 	return sub {$x};
>     }
> }
> print foo()->();

Well, I would expect it to output 'foo' on both occasions, and I'm
more than a little surprised to discover that it doesn't. Looks like a
bug to me.

-- 
Piers Cawley
www.iterative-software.com


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