Michael Fowler wrote:
>
> On Wed, Sep 20, 2000 at 07:45:16PM -0000, Perl6 RFC Librarian wrote:
> > "VARIABLE used only once: possible typo" should be replaced with
> > warnings on uses of uninitialized variables (including lexicals).
>
> > $x = 3
>
> I don't understand, who's to say you didn't misspell $x? If you're only
> using it once wouldn't that be likely? Who writes a program consisting of
> only assignments to variables that are never used, and expects not to see
> the "VARIABLE used only once: possible typo" warning?
My new warnings are more specific than the old warning. Perhaps I should
make the "variable initialized but never used" warning suggest a
possible typo?
> > complains, but
> >
> > $x = 3; $x = 3
>
> As it shouldn't; you've mentioned $x twice, which means you probably didn't
> misspell it. That your mentioning twice in this manner is silly is beyond
> perl's grasp.
Actually, it isn't. I've often written $x = $x = 3 simply to avoid this
warning, because in the current implementation perl can't several of the
different ways of using a variable.
But I think you're missing the point. Why would you assign a value to a
variable, but then never use that variable's value? All typos would be
caught by the "initialized but never used" warning, as well as other
mistakes.
> > does not, nor does
> >
> > my $x = 3
>
> I would say that this should complain just as $x = 3 would.
Yes.
> > Note that if you currently depend on lexical variables having
> > undefined values, you would need to change C<my $x> to
> > C<my $x = undef>. This is a good thing.
>
> Whoa, are you saying that saying 'my $x' no longer means $x has the value
> undef? What value does it have? How can this be a good thing?
No. No semantics change. This RFC is... what's the opposite of
far-reaching? It changes no semantics. It only adds warnings. 'my $x'
still means $x's value is undefined (but please do not say "the _value_
undef").
This is probably the most controversial part of the RFC. I'm saying that
my $x;
...some code that might set $x to some value...
if (defined ($x)) { ... }
is bad, but
my $x = undef; # or my $x; undef $x;
...some code that might set $x to some value...
if (defined ($x)) { ... }
is ok. That's because I want to distinguish between initialized
variables and variables whose values are undefined, because I think I
can catch a lot of mistakes by doing so. But this distinction is only in
the compiler. This is the same as in gcc; in gcc, the compiler remembers
whether an integer variable has been initialized, and complains when you
use it. But at runtime, that variable has some numeric value (a junk
value, zero more often than not.) Perl's runtime specifically tracks
variables and expressions whose value is undefined, and does it well
enough that most people think of 'undef' as being just another value.