in reply to Why do I get a "used only once" warning here?

Well, presumably you are storing keys in %MyApp because you intend to check them at some other point in your package. As soon as that check is in place, the warning will go away on its own. I'm not sure why you'd want to suppress the warning unless you are literally going to store a variable that you will never check or use, and then I'm not sure why you'd want to do that. So I guess the question is, why is it necessary for you to set the value but not check it? Or are you only wanting to suppress warnings until such time as you get around to adding the code to check the variable?
  • Comment on Re: Why do I get a "used only once" warning here?

Replies are listed 'Best First'.
Re^2: Why do I get a "used only once" warning here?
by rovf (Priest) on Mar 13, 2009 at 14:01 UTC
    presumably you are storing keys in %MyApp because you intend to check them at some other point in your package.

    Actually, I do check them, but not at another point in the package, but at the same. Note the usage of ||=.

    -- 
    Ronald Fischer <ynnor@mm.st>
      Ah, I missed the side-effect of the function, sorry. In fairness, Perl has no way of knowing (as I should have from the name of the function, "log") that your function has a side-effect. If this function were to simply return a value, then the warning would make sense, because you would be storing a value and never using it. With the side-effect, though, everything becomes clear.

      If you really want that syntax, then you should turn warnings off as others have suggested. Alternatively, you could use a syntax which mentions the variable twice. I don't know if the fact that I missed the meaning of your code is indicative of unclear syntax, or if it's just me being blind this morning.
        erl has no way of knowing (as I should have from the name of the function, "log") that your function has a side-effect.

        This is correct, but if we use an operator like ||=, &&=, .= and so on, implies that at least at that statement, the value of the variable is read AND written, so one could argue that this means that the variable is used at least twice. This has nothing to do with whether or not the expression on the right-hand side has side effects. In other words:

        $P::x ||= (f(),1);
        produces a warning, while the equivalent
        $P::x = $P::x || (f(),1);
        does not.

        -- 
        Ronald Fischer <ynnor@mm.st>