in reply to Using "my" suppresses "Name used only once" warning?

The "used only once" warning isn't there to spot declared, but unused variables. They are harmless. The "used only once" warning is there to catch typos in variables - "use strict" won't catch typos in fully qualified variable names - which certainly can be harmfull.

Personally I think this warning is one of the more annoying ones. It triggers to many false positives (at least, for the way I code). I hate having to use local, our, or use vars just to quiet the warning.

I'm not sure what you mean by "using a string as a number". Scalars in Perl are both strings and numbers. But Perl does already warn you (if you have warning on) if you use a string that doesn't look like a number as an operand for an arithmetic operation.

$ perl -we '"foo" + "bar"' Argument "bar" isn't numeric in addition (+) at -e line 1. Argument "foo" isn't numeric in addition (+) at -e line 1.

Abigail

Replies are listed 'Best First'.
Re: Re: Using "my" suppresses "Name used only once" warning?
by Wysardry (Pilgrim) on Feb 03, 2003 at 00:10 UTC

    I tend to want to pre-declare variables, as it's how I learnt in BASIC, C and COBOL. I also got into the habit of defining them before doing anything else.

    Obviously in large programs this can lead to some being declared but never used, or left behind when the code that uses them is removed.

    The using a string as a number thing was because I was testing what warnings were given and set a variable to 1 (numeric), concanated it with "1" with the . operand and then added a numeric number 1 to it, without getting any warnings about it.

    I know a "make your mind up about the context of $such_and_such" warning is too much to ask, but I'd have expected some sort of non-fatal complaint.

    I guess what I'm really after is a psuedo strong type casting option for variables. In other words, if I used a scalar in string context, I want to be warned if I later use it as a number (even if it looks like a number).

      I guess what I'm really after is a psuedo strong type casting option for variables. In other words, if I used a scalar in string context, I want to be warned if I later use it as a number (even if it looks like a number).

      Then you shouldn't have used Perl to begin with. This automatic casting between numbers and strings is a feature, just as it's considered a feature in C that if you divide a float by an integer, the integer is casted to a float.

      Also realize that in Perl, that any checking of the form "is this a number" has to be done at run-time (unlike the typechecking in C that can be done in compile time). You really don't want to do a check for each operation, the slowdown would be noticeable. You get the "Argument 'foo' isn't numeric" for 'free', because Perl needs to convert the string value to a numeric value, and the C library functions strto* will flag failure. You will notice that you get the warning only if Perl hasn't attempted to translate the string value to a numeric value - if Perl already did this (unsuccesfully), you don't get a warning.

      Abigail

        My main use for Perl is Web based applications, so the only other alternative is PHP and I don't consider that to be anywhere near as stable besides anything else.

        This check wouldn't have to be done at run-time - a separate "lint" program would do. I'd really only want to use the "ultra pedantic" check after completing a large section of code, not every time it was executed.

      The using a string as a number thing was because I was testing what warnings were given and set a variable to 1 (numeric), concanated it with "1" with the . operand and then added a numeric number 1 to it, without getting any warnings about it.

      This seems reasonable to me. Just assume that any time you do math with strings, or string operations with numbers that perl silently handles the type conversions required. So the below

      >perl -e "print 1 . '1' + '.01'" 11.01
      can be implictily read as
      >perl -e "print str2num(num2str(1) . '1') + str2num('.01')"
      but is much less confusing.

      Dont knock it, eventually youll wonder why it isnt this easy in all of the other languages...

      ;-)

      --- demerphq
      my friends call me, usually because I'm late....