in reply to -w, strict, and Tk...

Of course, warnings complains that "The variable is used only once."

This error pops up occasionally, usually for the right reason but occasionally, (such as where you declare a variable in your script but only acess the variable from within an eval), quite incorrectly.

The solution is usually simple:

use warnings; use strict; our $x; eval (' $x=1; print $x; '); #Generates a warning that $x is only used once.. Incorrectly. # {my $work_around=$x} # Uncomment to make the error go away.
By adding the anonymous block with the (useless but for its side effect) lexical declaration and assignment of $x, the compiler has now seen the variable twice and the warning goes away, but with little cost or impact on your script. (In fact it wouldnt suprise me in the least if the compiler optimizes it away, _after_ the lexical analysis phase)

If you had to make a bunch of these disappear then maybe

{ my @work_around=($x,$y,$z); }
Would be the neatest way to resolve them all in one workaround.

Yves / DeMerphq
--
This space for rent.

Replies are listed 'Best First'.
Re: Re: -w, strict, and Tk...
by greenFox (Vicar) on Mar 20, 2002 at 13:58 UTC

    Am I the only one who finds it a bit odd that you can remove a warning for a variable that is only used once by creating another variable that is... only used once???

    Why doesn't $work_around generate the same error it was introduced to stop?

    --
    my $chainsaw = 'Perl';

      Because it's lexical? The "only used once" warning only pops up for globals, I think.
      --
      Mike