Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: How do I find out which variables I'm not using?

by Aighearach (Initiate)
on Nov 06, 2004 at 02:30 UTC ( [id://405683]=note: print w/replies, xml ) Need Help??

in reply to Re: How do I find out which variables I'm not using?
in thread How do I find out which variables I'm not using?

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.


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

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

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



      Yes nice! But note that, you don't need to use warnings qw(once) because the warnings provided by the warnings pragma are lexical, and you've got them enclosed in that block. So there IS some overkill there. I do like the block, so I wouldn't call the most of it overkill.

      But does NAMING the block ever tempt you to goto LABEL??? I always am tempted, and grin mischieviously, and then I have to say 100 Hail Larry's to forgive myself.

      Snazzy tagline here

        Both the label and the use() statement are technically unnecessary; or, conversely, the label and the block are unnecessary. I am aware of this but use them anyway. It's all part of the defensive progamming strategy.

        I would rather <insert your own nightmarish ineffability here> than type goto LABEL :-)



Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://405683]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-06-18 08:49 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.