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

Re: explicitly declare closures???

Thread Previous | Thread Next
Piers Cawley
August 21, 2001 09:33
Re: explicitly declare closures???
Message ID:
Dave Mitchell <> 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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About