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

Hi all,

I'm using a guard object, and in simple cases where the guard is simply created and then left to it's devices, you get a useless "Useless" warning. Eg:

... my $guard = My::Guard->new(sub { $sth->finish }); ...

Yields the warning:

Useless use of private variable in void context at <snip> line 169. Of course I can suppress this warning with no warnings 'void', but I'm vaguely paranoid that in some circumstance or perl version, the variable might be optimised out of existence thus breaking the guard object use.

So I have two questions:

1. Any smart way to deal with the warning? Can I suppress it from within the guard package (which is by definition in a different scope to it's user)

2. Anyone know if the variable would ever be optimised away?

Update: My bad, it is not the simple assignment above which generates the warning, it's when I chain guards to preserve order. eg:

... my $guard1 = My::Guard->new(sub { $sth->finish }); ... some code which needs the protection of guard1 ... my $guard2 = My::Guard->new(sub { .. clear up something else ..; $guar +d1; }); ... some code which needs the protection of both guards, and the order + of resource clearing is important ...

Replies are listed 'Best First'.
Re: useless Useless warning
by moritz (Cardinal) on Jun 24, 2008 at 08:06 UTC
    When I run your code I don't get that warning. Please try to post a small script that we can execute to reproduce the warnings. Then we can actually tell you how to avoid it.
      my bad - updated the original node with the code that actually causes the warning.
Re: useless Useless warning
by Anonymous Monk on Jun 24, 2008 at 09:23 UTC
    C:\>perl -we"{my $pop;}" C:\>perl -we"{my $pop;$pop;}" seless use of private variable in void context at -e line 1. C:\>perl -Mdiagnostics -we"{my $pop;$pop;}" Useless use of private variable in void context at -e line 1 (#1) (W void) You did something without a side effect in a context that +does nothing with the return value, such as a statement that doesn't ret +urn a value from a block, or the left side of a scalar comma operator. V +ery often this points not to stupidity on your part, but a failure of P +erl to parse your program the way you thought it would. For example, y +ou'd get this if you mixed up your C precedence with Python precedence a +nd said $one, $two = 1, 2; when you meant to say ($one, $two) = (1, 2); Another common error is to use ordinary parentheses to construct a +list reference when you should be using square or curly brackets, for example, if you say $array = (1,2); when you should have said $array = [1,2]; The square brackets explicitly turn a list value into a scalar valu +e, while parentheses do not. So when a parenthesized list is evaluate +d in a scalar context, the comma is treated like C's comma operator, whi +ch throws away the left argument, which is not what you want. See perlref for more on this. This warning will not be issued for numerical constants equal to 0 +or 1 since they are often used in statements like 1 while sub_with_side_effects() ; String constants that would normally evaluate to 0 or 1 are warned about. C:\>
      Thanks - I hadn't looked at the diagnostics output.

      So really the problem is in the assumption that there is no side effect, wheras in fact there is. The side effect is that the variable is made present in a a new scope which prevents the object from being destroyed (and the destructor has side effects).

      Hm, or does it. Since I'm not assigning it to a variable the refcount won't be being increased. But surely it can't be destroyed while it's still in scope - in a void context or not...

      I'll test it out and report back.

Re: useless Useless warning
by aufflick (Deacon) on Jul 13, 2008 at 12:03 UTC
    Oh man - this will teach me to be lazy when posting expurgated snippits of code that don't correctly exhibit the problem.

    Since posting this message I have lost about 100xp! Strangely, the node itself is only -1, so I guess someone is upset/annoyed and going on a bit of a seek and destroy mission.

    Who said XP doesn't encourage good question writing - I won't be making this mistake again ;)