James Mastros wrote:
Juerd wrote:
Just typing "my " before the first use of a variable isn't hard, and it
makes things much clearer for both the programmer and the machine.
Does this imply that it's now possible to type C<my @foo[23] = 42;>, and declare @foo? In the current perl, this doesn't work -- it's a syntax error. It'd certainly make many constructs easier.
I see why that's an error. It can be very confusing.... In literal fashion you are attempting to make the 24th element of @foo lexical, and having some elements of @foo have different scope from the rest is a bad idea.
However, I have also been bitten by that rather frequently, though with a different construct. Typically it's with hash slices:
my @[EMAIL PROTECTED] = @[EMAIL PROTECTED]; # ERROR!
my %newhash; @[EMAIL PROTECTED] = @[EMAIL PROTECTED]; # Okay, but not as convienent.
This was part of the reason I spawned this thread. But it was also due to being in the middle of trying to write some decent quality code under a heavy time pressure, and noticed that >50% of my lines had a 'my' on them. It's typically not that high of a percentage, but when you are creating lots of small utility routines, declaring all your lexicals can be a significant part of the task. I added to this the observation that lexical variables are significantly more common than non-lexicals, and thought out loud as to why I was doing more work for the common case than the uncommon case, in a language that generally doesn't have that problem.
One of the other reasons in favor of the idea was aesthetic.
# stuff which declares $x, $z, and $q
$x = 4; my $y = 7; $z = 12; my $r = 4543; $q = 121;
compared to:
# stuff which declares $x, $z, and $q
$x = 4; $y = 7; $z = 12; $r = 4543; $q = 121;
With a fixed width font, like all code editors use, all the =' like up, and I can quickly scan the var names to get to the one I want to change at that moment. Yes, I could have added a 'my ($y, $r);' to the front of the list, but that's adding another statement to the mix, same as the hash slice above, adding considerably more weight to 'my' than just three keystrokes (m - y - space).
However, given the strong opposition (with merits) to this in other responses, I am willing to live with it the P5 way. Just seemed like autolexicals was rather DWIMish.
Another facet of this discussion comes into account when also specifying type.
from S9: my bit @bits; my int @ints; my num @nums; my int4 @nybbles; my str @buffers; my ref[Array] @ragged2d; my complex128 @longdoublecomplex;
Wouldn't this be much better as: bit @bits; int @ints; num @nums; int4 @nybbles; str @buffers; ref[Array] @ragged2d; complex128 @longdoublecomplex;
Given that most of the stated reservations had to deal with explicit declaration better defining scope, what is wrong with drooping the my in this case?
-- Rod Adams
