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

Hi All

I have made a script with a subroutine that swaps tags in a html file with variables - but when I run perl -w script.pl I get lots of these errors:
Name "main::download" used only once: possible typo at script.pl line +46.

Any ideas how to get round this? Thanks

Replies are listed 'Best First'.
Re: -w Error
by mpolo (Chaplain) on Nov 18, 2001 at 19:23 UTC
    The error is saying that you have defined a variable named download and never used it. The assumption that -w is making is that you will use every variable after defining it. This is supposed to catch typos like this:
    $download=5; print $dwonload;
    which is perfectly valid, but doesn't do what you want it to do...
Re: -w Error
by {NULE} (Hermit) on Nov 18, 2001 at 22:43 UTC
    Hi,

    I have two suggestions for you - one of which you may already know. First is that if you have not put use strict in your code, you really should. Most of these you can track down with the extra messages that use strict; presents.

    If you have a legitimate condition where a value is used only once then there is another trick you can use to get rid of these: create a subroutine that is never called and use the variables in there:

    # You never, ever want to call this. sub perl_unused_by_me { # You have to make sure your scope is correct here. $main::variable = 0; # You might also consider something like # undef $main::variable; # so that if this gets called by accident then your # code will produce an error at that point, not # silently use the bad data. }
    Important: I really believe that if you need to do this, then there is something wrong with the way you have approached your program design. I found that as my skill increases situations where I have to do this have disappeared. In my mind that means I was initially doing things the wrong way. Usually that means using globals where passing by reference, even some slick OO or something (anything) more attractive would get the job done.

    Good luck,
    {NULE}
    --
    http://www.nule.org

Re: -w Error
by jlongino (Parson) on Nov 18, 2001 at 22:10 UTC
    I got similar messages when I was playing around with File::Find and Merlyn responded to it. I had asked for an explanation of how to code it "warning free", but no one came forward with a solution. So I took Merlyns advice and didn't worry about it. It would probably be a good idea to post the code and have it critiqued first just to make sure that there isn't a real underlying problem though.

    --Jim