Abigail said:
(e-mail address removed)-berlin.de (
[email protected]) wrote
on MMMMCMXCVI September MCMXCIII in <URL:
> Michele Dondi (
[email protected]) wrote on MMMMCMXCIV September
> MCMXCIII in <URL:
> <>
> <> >That '@a' doesn't warn in your example has little to do with @a being
> <> >special. It just has to do with the fact that *a exists. Which exists
> <>
> <> That's surely what Anno meant. As you surely know full well.
>
>
> Then I don't get it. @a behaves the same as @c.
True, but the specialness of $a (the package variable) makes it easier
to use it inadvertently. That way @c is slightly safer than @a.
I still fail to get it. You seem to be saying "because $a is special,
you might not get the warning @main::a used only once". But that is
only a useful warning if you *mistype* a variable name - in which case
it's safer to use @a (which if mistyped to @c triggers the warning -
or triggers a strict error) than to use @c (which if mistypes to @a may
not trigger the warning or error). But that doesn't make much sense.
The advice would have to be never to misspell "a" or "b" when you intend
to write something else. I guess you got me there.
So the use of @a, @b, %a, and %b as package variables (or lexicals)
seems to be technically no problem. The lexicals $a and $b should be
avoided if there is a chance you want to run sort() in the same
scope. There always is; you'd have to use a prototyped comparison
function, a major nuisance.
If one is interested in them at all, the package variables $::a and
$::b are less problematic than they would seem to be. If used in a
sub, you must localize them, but that goes for all package variables
that aren't meant to be global. $a and $b are only special in that
they can't reasonably be used globally.
The reason to avoid them would be that from "perldoc -f sort" one
would expect sort() to overwrite them randomly. But that doesn't
happen:
( $a, $b) = ( 99, 100);
my @s = sort { $b <=> $a } 1 .. 4;
print "$a $b\n";
still prints "99, 100". The preserving behavior has been around for
a long time, but it isn't documented or I overlooked it more than once.
Apart from that, there doesn't seem to be much reason to avoid $::a
and $::b at all, except, of course, who wants them in the first place.
Lexical $a and $b would be nice, but they remain problematic.
Anno