Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do I find out which variables I'm not using?

by amyoungil (Initiate)
on Nov 06, 2004 at 00:07 UTC ( #405672=perlquestion: print w/replies, xml ) Need Help??

amyoungil has asked for the wisdom of the Perl Monks concerning the following question:

I tried defining $moosie, then never using it, to see if it would throw a warning, but it didn't. I thought that it used to do that? Maybe I'm wrong. I am using -w....
I'd like to know this so I can clean up my programs.

From MonkDan at UI:
$ perl -V:version version='5.8.3';
$ perl -e '$moosie = 42'
$ perl -we '$moosie = 42' Name "main::moosie" used only once: possible typo at -e line 1.
$ perl -We '$moosie = 42' Name "main::moosie" used only once: possible typo at -e line 1.
$ perl -We 'my $moosie = 42'
It looks like if you use `my', you can't find out which ones you aren't using.
  • Comment on How do I find out which variables I'm not using?

Replies are listed 'Best First'.
Re: How do I find out which variables I'm not using?
by Prior Nacre V (Hermit) on Nov 06, 2004 at 02:26 UTC

    The same question is currently being discussed: Stray Variables.

    merlyn's reply addresses the lexical variable issue.

    Regards,

    PN5

Re: How do I find out which variables I'm not using?
by Prior Nacre V (Hermit) on Nov 06, 2004 at 01:34 UTC

    Casting my mind back a decade or so to Perl4, I do recall having to write code like this to stop the warning messages:

    $x || $y || $z || 1;

    What you say about my variables appears to be right on the mark. Here's some tests:

    [ ~/tmp ] $ perl -we 'use strict; my $x' [ ~/tmp ] $ perl -we 'my $x' [ ~/tmp ] $ perl -we '$x' Useless use of a variable in void context at -e line 1. Name "main::x" used only once: possible typo at -e line 1. [ ~/tmp ] $ perl -we 'no warnings qw(once); $x' [ ~/tmp ] $

    I would tentatively suggest that the reason stems from the fact that lexically scoped variables are stored in the scratchpad while dynamically scoped variables are stored in the symbol table. Presumably the used only once test is only performed on the symbol table.

    Another monk, more familiar with Perl's internals, may be able to provide the reason for this. Anyone?

    Regards,

    PN5

      I'm no internals wizard, but I think you're right on the mark.

      But more importantly, this is the correct behavior. The purpose of the warning is (presumably) a second line of defense behind strict, for detecting a mispelled variable names. It's purpose probably isn't to warn you that you didn't use it enough, but rather, that not using an undeclared variable means you may (probably do) have a typo.

      In the case of a my declared variable, You'll actually still get the warning if you typo it OTHER THAN in the my call. But if you're using my... shouldn't you also be using strict? I am curious as to the thought process that would lead to using only one of the two.

      Examples:

      perl -we 'my $foo; $fooo = 42' Name "main::fooo" used only once: possible typo at -e line 1.
      Typo was still detected.
      perl -we 'my $fooo; $foo = 42' Name "main::foo" used only once: possible typo at -e line 1.
      Typo still detected.
      perl -we 'my $fooo;'
      No warning. If it is a typo is questionable. It is like a program with no output; if it DOES anything is a matter for some philosophers. But it doesn't matter. Declaring an extra my var doesn't hurt anything. And if you group all your my calls together like my( $foo, $bar, $baz, ) then it doesn't even waste much time. On many systems, I doubt the difference would be measurable.

      And so even in all the examples given prior, the correct behavior of identifying harmful typos is always taken.

      Personally, I think it's terrible to write code without strict, when it's code that is important enough, or will last long enough, or is intricate enough, that warnings are needed or used. I would only recommend forgoing use strict on once-off one-liners (ie, system administration and playing around).


      --
      Snazzy tagline here

        I agree with your sentiments on strict.

        I often test things like regexes from the commandline before adding to application code. I always include warnings and strictures in these tests:

        perl -we 'use strict; ...'

        Whenever I fiddle with these in a script or module, I tend to go into blatently-obvious-defensive-progamming mode:

        NO_ONCE_ONLY_WARNINGS_ZONE: { no warnings qw(once); . . . use warnings qw(once); }

        I am often told this is overkill: usually by people that do a lot more debugging than me :-)

        Regards,

        PN5

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://405672]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-07-01 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (99 votes). Check out past polls.

    Notices?