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);