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

Re: explicitly declare closures???

Thread Previous | Thread Next
Mark-Jason Dominus
September 4, 2001 11:37
Re: explicitly declare closures???
Message ID:

Says Dave Mitchell:

> Closures ... 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()->();

That is confusing, but it is not because closures are confusing.  It
is confusing because it is a BUG.  In Perl 5, named subroutines are
not properly closed.

If the bug were fixed, the result would be 'bar' regardless of whether
or not $x was commented.

This would solve the  problems with mod_perl also.

The right way to fix this is not to eliminate closures, or to require
declarations.  The right way to fix this is to FIX THE BUG.

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