in reply to Warnings on unused variables?
There is such a warning ("once") for package variables
>perl -c -we"print $a" Name "main::a" used only once: possible typo at -e line 1.
On the p5p list, there was talk of doing something similar for lexical variables. It's a very long thread, so what follows is a highlight.
First, it seems there's no real good reason. Quote demerphq,
Used once warnings are irritating and IMO unnecessary for lexicals.
The reason globals have the warning is that it is easy to make a typo that changes program behaviour without being notified of it any other way.
$Foo::Baar=1; print "Foo::Bar is enabled" if $Foo::Bar;Strictures will not catch this, in fact, nothing but "used once" errors will catch this.
However notice that under strictures its impossible to construct the same scenario with lexicals as one of the two usages will be an undeclared variable error.
Furthermore, it seems there are too many legit uses of single-use lexical variables to add a warning. Some examples from the thread:
my $foo = 42; eval q[$foo++];my($unused, $foo) = func();{ open my $fh, ">$file" or die $! } # touch{ my $lock = lock_something(); # lock released on scope exit do_stuff_that_needs_the_lock(); }{ my $tree = ...; my $sentry = Object::Destroyer->new( $tree, 'delete' ); ... }my $object = bless \do {my $var} => $class;
Due to all the false positives, it seems the general consensus is that the check should done by a linter (e.g. Perl::Critic).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Warnings on unused variables?
by AZed (Monk) on Sep 27, 2008 at 13:42 UTC | |
by ikegami (Patriarch) on Sep 27, 2008 at 18:27 UTC | |
by AZed (Monk) on Sep 27, 2008 at 21:19 UTC | |
by Corion (Patriarch) on Sep 27, 2008 at 21:23 UTC | |
by AZed (Monk) on Sep 27, 2008 at 22:20 UTC | |
| |
by ikegami (Patriarch) on Sep 27, 2008 at 22:58 UTC | |
by AZed (Monk) on Sep 28, 2008 at 00:32 UTC | |
| |
by ikegami (Patriarch) on Sep 27, 2008 at 23:02 UTC | |
by AZed (Monk) on Sep 28, 2008 at 00:46 UTC | |
| |
|
Re^2: Warnings on unused variables?
by Animator (Hermit) on Sep 28, 2008 at 12:46 UTC | |
by AZed (Monk) on Sep 28, 2008 at 16:02 UTC | |
by Animator (Hermit) on Sep 29, 2008 at 19:22 UTC | |
by AZed (Monk) on Oct 02, 2008 at 01:18 UTC | |
by Animator (Hermit) on Oct 02, 2008 at 07:08 UTC | |
| |
by ikegami (Patriarch) on Sep 28, 2008 at 21:12 UTC |