Bart Lateur wrote:
>
> On Mon, 7 Aug 2000 15:47:22 -0400, Ted Ashton wrote:
>
> >$x = 2;
> >{
> > $x = 1;
> >}
> >print "$x\n";
>
> >But if lexicals were the default, then the same code would print
> >
> >2
> >
> >because it would be equivalent to todays:
> >
> >my $x = 2;
> >{
> > my $x = 1;
> >}
> >print "$x\n";
> >
> >Would it not?
>
> Wow. Hold it. Some people on this list have written that they want this
> to mean:
>
> my $x = 2;
> {
> $x = 1;
> }
> print "$x\n";
>
> which is also using lexicals, only, the scope is wide enough to include
> all occurences. This makes it almost identical to global variables.
>
> IMO this shows this "default to lexicals" isn't the ultimate solution.
Shouldn't we be shooting for some sort of readily apparent clarity? If I
type:
$x = 1;
if ( $foo ) {
$x = 2;
}
The first thing I'm going to expect is that $x is going to be 2 coming
out of the loop. It's the easiest answer looking at the code. If I want
the thing in the loop to 'be' lexical, I should make it so explicitly,
or use a different variable name. (The real simplest thing.)
Someone earlier said that the real problem would manifest itself in
multiple-file programs. I agree, because this is where you don't know if
someone is using a variable with, thus the simple, 'use a different
name' solution wouldn't work. Lexicality 'really' only matters when you
are writing modules. (someone could write a program using your code
without your code being in their editor) I would contend, that if you
are to the point of writing modules and manipulating package namespaces
and you can't figure out lexical variables, that you need to take a step
back. When you're progamming a module, i.e. this code will be reused, in
ways you probably aren't planning on and you are creating and presenting
an interface to the world, you have to be more deliberate and thoughtful
with your programming.
One of the current great things about Perl is that you don't have to
deal with namespaces and lexicality just to write a script to parse
/etc/passwd. Let's not screw that up in an attempt to make lazy module
writing easier. The sloppy programmers are not the ones who should be
writing modules anyway.
Monty