[% setvar title variable usage warnings %]
|Note: these documents may be out of date. Do not use as reference!|
To see what is currently happening visit http://www.perl6.org/
variable usage warnings
Maintainer: Steve Fink <email@example.com> Date: 2 Aug 2000 Last Modified: 20 Sep 2000 Mailing List: firstname.lastname@example.org Number: 12 Version: 3 Status: Developing
"VARIABLE used only once: possible typo" should be replaced with warnings on uses of uninitialized variables (including lexicals).
v2: Separated "definitions without uses" from "uses without definitions" (def-use and use-def chains).
v3: Added example and attempted to improve the explanation.
Perl6 should distinguish between uses and assignments to variables, and warn only when a variable is used without being assigned, or assigned to without being used. In perl5 the complete program
$x = 3
$x = 3; $x = 3
does not, nor does
my $x = 3
use vars qw($x $y); $x = $z; $y = $z;
It would be more useful to have a complaint for both lexical and package variables, and only when a variable is used without ever being assigned to (or having its reference taken), or assigned to without its value ever being used.
The warning for the use of an unassigned variable should be "use of
$x". This message is too close to the
existing "use of uninitialized value", but that message is badly
phrased anyway, so it will change to "use of undefined value" to
better reflect its actual meaning. (The two are related; "use of
undefined value" can be thought of as encompassing the runtime
counterpart to the compile-time "use of uninitialized variable"
proposed here.) The assignment of a variable which is never used will
result in "variable
$x defined but never used".
If whether a variable is initialized is conditional upon the control
flow and thus unknowable at compile time, the warning is "possible use
of uninitialized variable
Note that if you currently depend on lexical variables having
undefined values, you would need to change
my $x to
my $x = undef. This is a good thing.
1 my ($x, $y, $z, $r); 2 $z = 1; 3 f(\$r); 4 my $logfile = "/tmp/log"; 5 $x = 1 if cond(); 6 print $x+$y; 7 undef $z; 8 print $z; --> possible use of uninitialized variable $x in line 6 (compile time) --> use of uninitialized variable $y in line 6 (compile time) --> variable $logfile defined in line 4 but never used (compile time) --> use of undefined value in line 8 (run time)
No warning is issued for
$r because any variable whose reference is
taken may be used and/or assign to through the reference.
I have no idea how difficult this would be to implement. You just need
to distinguish between lvalue and rvalue uses of a variable, I guess?
But hey, this is a language RFC, not an internals RFC. :-) There's
also the question of whether to properly track uses and definitions so
$::z = $x; $x = 3 is a warning, as well as
$x = 3 if f(); print $x. Though the latter would require renaming
the warning to "possible use of uninitialized variable
Glenn Linderman <Glenn@Linderman.com> - definitions without uses