Warning about unused lexical variables

  • Thread starter Peter J. Holzer
  • Start date
P

Peter J. Holzer

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
 
M

Michele Dondi

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 <http://perlmonks.org/?node_id=636990>. I'll update
this thread as replies get posted there.


Michele
 
M

Michele Dondi

I don't have the slightest idea about how to answer your question, but
I duplicated at <http://perlmonks.org/?node_id=636990>. 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-destruction.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);
 
M

Michele Dondi

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
 
J

Jürgen Exner

Peter said:
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 (<some complex condition depending on the environment>) {
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
 
P

Peter J. Holzer

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 (<some complex condition depending on the environment>) {
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 (<some complex condition depending on the environment>) {
my $x = 1;
call_my_sub($x)
} else {
my $y = 2;
call_my_sub($y)
}

hp
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top