Dave Mitchell <[EMAIL PROTECTED]> wrote:
> John Porter <[EMAIL PROTECTED]> wrote:
> > Dave Mitchell wrote:
> > > I think closures are a lot harder (or at least subtler) than
> > > people think ...
> >
> > ... The scenario you gave seems rather far-fetched to me, in terms
> > of real-world programming.
>
> Perhaps, although the following code demonstrates it, and isn't (too)
> contrived. Comment out the 'superfluous' "$counters[$i] = 0;" line and
> the code stops working.
We must be very careful not to confuse "closure" with "Perl's
current implementation of closure". You've stumbled onto a bug in
Perl, not discovered a feature of closures. Perl's "closures"
were horribly buggy until release 5.004. (Thanks Chip!)
Closed variables are just local variables. There's nothing special
about refering to a variable from an inner scope. You don't want
to write
sub foo {
my $x;
if (...) { my outer $x; $x = 0 }
else { my outer $x; $x = 1 }
$x;
}
do you? So why make people using closures do it?
My only closure bug due to scoping was because I named a variable
poorly and accidentally shadowed it in an inner scope. Your
proposal does nothing to eliminate that bug.
A more common problem I have is with reading code that uses a
variable a long way from where it was introduced. Your proposal
adds a nearby declaration, but it's a useless declaration that
doesn't help find the original variable.
IMHO, every declaration we write should make a program more
understandable by *people*. If the only reason we declare
something is to help the computer understand it, we should think
carefully on how to eliminate the declaration.
- Ken