"my" suppresses "used only once" warnings

J

jidanni

Here we see "my" suppresses "used only once" warnings.
And I thought "my" was all good.

$ perl -we 'use diagnostics; $g=1; my $h=2;'
Name "main::g" used only once: possible typo at -e line 1 (#1)
(W once) Typographical errors often show up as unique variable names.
If you had a good reason for having a unique name, then just mention it
again somehow to suppress the message. The our declaration is
provided for this purpose.

NOTE: This warning detects symbols that have been used only once so $c, @c,
%c, *c, &c, sub c{}, c(), and c (the filehandle or format) are considered
the same; if a program uses $c only once but also uses any of the others it
will not trigger this warning.

What about $h? "perldoc -f my" doesn't warn about no warnings. Even
perlsub doesn't say...
 
A

A. Sinan Unur

Here we see "my" suppresses "used only once" warnings.
And I thought "my" was all good.

$ perl -we 'use diagnostics; $g=1; my $h=2;'
Name "main::g" used only once: possible typo at -e line 1 (#1)
(W once) Typographical errors often show up as unique variable
names. If you had a good reason for having a unique name, then
just mention it again somehow to suppress the message. The our
declaration is provided for this purpose.

NOTE: This warning detects symbols that have been used only once
so $c, @c, %c, *c, &c, sub c{}, c(), and c (the filehandle or
format) are considered the same; if a program uses $c only once
but also uses any of the others it will not trigger this warning.

The "used once" warning is useful for detecting things like (when you
are not using strict -- and you should use strict):

$STUPIDO = 1;

# 100 more lines

$STUPID0 = 0;

When you use strict, you have to declare the variables you use. If you
write:

my $STUPIDO = 1;

why should you get a warning? After all, you have explicitly told the
compiler, you want a variable named $STUPIDO. The same goes for

my $STUPID0 = 0;

If you use strict as you should, you will find out about typos:

use strict;

my $STUPIDO = 1;

# 100 more lines

$STUPIDO = 0;

Now, if you do

my $STUPIDO = 1;

# 100 more lines

my $STUPIDO = 0;

you will also get an appropriate message.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top