Warning about unused lexical variables
Warning about unused lexical variables
am 04.09.2007 12:25:30 von hjp-usenet2
Occasionally I notice a lexical variable sticking around which isn't
used any more and can/should be deleted, like in this (stupid) example:
#!/usr/bin/perl
use warnings;
use strict;
my $x;
my $y;
$y = 42;
print "$y\n";
__END__
This can be made slightly more complex by changing "my $x" to
my $x = compute_some_value();
where $x is assigned a value which is subsequently never used.
It would be nice if perl could warn about lexical variables which are
never used in their scope after their initialization. Does anybody else
find this useful, and if so, is there a reason (besides "life is short")
why it hasn't been implemented?
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
Re: Warning about unused lexical variables
am 04.09.2007 19:48:05 von Michele Dondi
On Tue, 4 Sep 2007 12:25:30 +0200, "Peter J. Holzer"
wrote:
>It would be nice if perl could warn about lexical variables which are
>never used in their scope after their initialization. Does anybody else
>find this useful, and if so, is there a reason (besides "life is short")
>why it hasn't been implemented?
I don't have the slightest idea about how to answer your question, but
I duplicated at . I'll update
this thread as replies get posted there.
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Warning about unused lexical variables
am 05.09.2007 05:09:24 von attn.steven.kuo
On Sep 4, 3:25 am, "Peter J. Holzer" wrote:
> Occasionally I notice a lexical variable sticking around which isn't
> used any more and can/should be deleted, like in this (stupid) example:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $x;
> my $y;
>
> $y = 42;
> print "$y\n";
> __END__
>
> This can be made slightly more complex by changing "my $x" to
>
> my $x = compute_some_value();
>
> where $x is assigned a value which is subsequently never used.
>
> It would be nice if perl could warn about lexical variables which are
> never used in their scope after their initialization. Does anybody else
> find this useful, and if so, is there a reason (besides "life is short")
> why it hasn't been implemented?
>
> hp
>
You could use perl's backend to dump the lexical
variables and the line numbers on which they
appear:
$ nl -ba foo.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my $x;
6 my $y;
7
8 $y = 42;
9 print "$y\n";
10
$ perl -MO=Xref foo.pl
File foo.pl
Subroutine (definitions)
Package Internals
&HvREHASH s0
&SvREADONLY s0
&SvREFCNT s0
&hash_seed s0
&hv_clear_placeholders s0
&rehash_seed s0
Package PerlIO
&get_layers s0
Package Regexp
&DESTROY s0
Package UNIVERSAL
&VERSION s0
&can s0
&isa s0
Subroutine (main)
Package (lexical)
$x i5
$y i6, 8, 9
foo.pl syntax OK
This indicates that $x only appears on line 5.
--
Hope this helps,
Steven
Re: Warning about unused lexical variables
am 05.09.2007 18:53:28 von Michele Dondi
On Tue, 04 Sep 2007 19:48:05 +0200, Michele Dondi
wrote:
>I don't have the slightest idea about how to answer your question, but
>I duplicated at . I'll update
>this thread as replies get posted there.
Here is what has been said:
Re: Warning about unused lexical variables
by toolic on Sep 04, 2007 at 20:11 GMT-2
I also find this useful.
A few weeks ago, something prompted me to run a Super Search on
"unused variable". I did not find the answer to this question, but the
most relevant node seemed to be "Devel::GC::Helper and unused
variables" (http://perlmonks.org/?node_id=602205).
Hopefully, a much wiser monk can enlighten us.
------------------------------------------------------------ ----------
Re: Warning about unused lexical variables
by sgt on Sep 04, 2007 at 20:27 GMT-2
B::Xref o B::Lint (with some plugin) can help I guess
------------------------------------------------------------ ----------
Re: Warning about unused lexical variables
by kyle on Sep 04, 2007 at 21:19 GMT-2
My (small) problem with this would be that the "unused" variable might
still serve some purpose. Consider a compute_some_value() like one of
these:
sub pay_attention {
die 'void context' if ! defined wantarray;
return rand;
}
sub side_effect {
print "I am happy.\n";
return;
}
sub wait_for_destruction {
return OnDestroyDo->new( \&side_effect );
}
In case that last one is not clear, the idea is that it returns an
object that will perform some action when it is destroyed.
Arguably a warning is still warranted in even these cases, but it
might not be easy to tell when an assigned value is really
superfluous.
------------------------------------------------------------ ----------
Re^2: Warning about unused lexical variables
by xdg on Sep 04, 2007 at 22:52 GMT-2
> the "unused" variable might still serve some purpose
Another example would be "anonymous" scalar references.
my $scalar_ref = \(my $s);
Or would taking a reference count as "using" it?
------------------------------------------------------------ ----------
Re^2: Warning about unused lexical variables
by bart on Sep 05, 2007 at 13:06 GMT-2
That's right. Check this article on perl.com on what this could be
used for: "Better Code Through Destruction"
(http://www.perl.com/pub/a/2007/06/07/better-code-through-de struction.html).
------------------------------------------------------------ ----------
Re: Warning about unused lexical variables
by Rhandom on Sep 04, 2007 at 22:20 GMT-2
At first glance it sounds like a great idea. And it is to a certain
extent. But there is an existing system in Perl that does that very
thing, although only with global variables.
[paul@paul-laptop nqp]$ perl -we '$x = 1'
Name "main::x" used only once: possible typo at -e line 1.
Speaking from experience, this warning has never helped me. It has
done the opposite. It has always been a hinderance. It has forced me
to write things similar to the following:
my $value = $SOME_PKG::SOME_VAL
|| $SOME_PKG::SOME_VAL; # warn clean
Arguably, I should have a function in SOME_PKG that returns the value
of $SOME_VAL. But there are many existing modules that don't provide
accessors.
Use strict catches all of my typos. A warning of this sort never has.
Still - I could see a use for it, but I think I'd want it as an
extra-extra pragma:
use warnings qw(single_use_vars);
------------------------------------------------------------ ----------
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Warning about unused lexical variables
am 06.09.2007 19:33:30 von Michele Dondi
On Wed, 05 Sep 2007 18:53:28 +0200, Michele Dondi
wrote:
>Here is what has been said:
Update:
Re: Warning about unused lexical variables
by bduggan on Sep 06, 2007 at 19:06 GMT-2
I was looking for this recently, and found it mentioned in
Perl::Critic::TODO (TBD::VariableNotUsed).
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Warning about unused lexical variables
am 06.09.2007 20:58:04 von jurgenex
Peter J. Holzer wrote:
> It would be nice if perl could warn about lexical variables which are
> never used in their scope after their initialization. Does anybody
> else find this useful, and if so, is there a reason (besides "life is
> short") why it hasn't been implemented?
Useful? Yes.
Easy to implement? No.
Example:
my ($x, $y) = (1,2);
if () {
call_my_sub($x)
} else {
call_my_sub($y)
}
In this example either $x or $y remains unused.
However a static analysis at compile time cannot detect this.
And neither can a dyamic analysis during runtime because that complex
condition may evaluate to false only in an odd one-in-a-million situation.
jue
Re: Warning about unused lexical variables
am 06.09.2007 21:27:38 von Jim Gibson
In article <05YDi.11825$Ov2.4092@trndny06>, Jürgen Exner
wrote:
> Peter J. Holzer wrote:
> > It would be nice if perl could warn about lexical variables which are
> > never used in their scope after their initialization. Does anybody
> > else find this useful, and if so, is there a reason (besides "life is
> > short") why it hasn't been implemented?
>
> Useful? Yes.
> Easy to implement? No.
>
> Example:
>
> my ($x, $y) = (1,2);
> if () {
> call_my_sub($x)
> } else {
> call_my_sub($y)
> }
>
> In this example either $x or $y remains unused.
> However a static analysis at compile time cannot detect this.
But both $x and $y appear in two separate instances within their scope.
Contrast that with:
my ($x, $y) = (1,2);
if () {
call_my_sub($x)
} else {
call_my_sub($z)
}
$y and $z both only appear once, so it looks like there has been a
typo. That is the type of error that could be caught with an analysis
of lexical variables.
>
> And neither can a dyamic analysis during runtime because that complex
> condition may evaluate to false only in an odd one-in-a-million situation.
A dynamic analysis is not needed.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: Warning about unused lexical variables
am 07.09.2007 16:54:26 von hjp-usenet2
On 2007-09-06 18:58, Jürgen Exner wrote:
> Peter J. Holzer wrote:
>> It would be nice if perl could warn about lexical variables which are
>> never used in their scope after their initialization. Does anybody
>> else find this useful, and if so, is there a reason (besides "life is
>> short") why it hasn't been implemented?
>
> Useful? Yes.
> Easy to implement? No.
I don't think it would be particularly hard to implement. That's a
rather common feature in compilers which generate machine code (IIRC
it's a byproduct of register allocation). But I don't know how expensive
it is - since perl compiles the source code every time it is run, we
want to avoid algorithms which can potentially take a long time.
> Example:
>
> my ($x, $y) = (1,2);
> if () {
> call_my_sub($x)
> } else {
> call_my_sub($y)
> }
>
> In this example either $x or $y remains unused.
In any particular run, yes. But both branches are possible, so you can
eliminate neither $x nor $y. So that code is ok, although it would
probably be cleaner to rearrange it as:
if () {
my $x = 1;
call_my_sub($x)
} else {
my $y = 2;
call_my_sub($y)
}
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | hjp@hjp.at |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"