Getting rid of perl's 'variable used only once' warnings


I generally include the -w option in my perl scripts because it makes debugging easier. But I also provide a -v option, via getopts, that I use as a flag for more detailed output. Then, within the script I'll have statements such as

($opt_v) && print "Doing such & such a task\n";

to help me debug. (There are other ways to do this, also.) However, the -w will give errors that the $opt_v variable is only being used once, even if called this way multiple times. To get rid of that particular error, one can add the following to the top of the script:

no warnings('once');

I would only add this later in the development of the script, because it can also mask legitimate warnings about about variables being pointlessly assigned a value and never being used again (such as if there is a typo).

10/15/2008