Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: Warnings on unused variables? by ikegami
in thread Warnings on unused variables? by AZed

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found