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

Maybe something like the following would suffice:

package MyModule; my %OneTime; sub f { $OneTime{+__FILE__.' '.__LINE__} ||= (print("logging\n"), 1); return(); } 1;

Replies are listed 'Best First'.
Re^2: Why do I get a "used only once" warning here?
by AnomalousMonk (Archbishop) on Mar 13, 2009 at 22:13 UTC
    The OPer seems to want to control one-time printing/logging from outside the package/module, so that approach would not seem to do the trick. (Also, I don't know what the  + in front of the  __FILE__ is supposed to do.)

    One can play around with caller to get a general solution closer to what I think the OPer wants:

      The OPer seems to want to control one-time printing/logging from outside the package/module

      If one really wants a global in some other package, then...

      package MyModule; use strict; use warnings; sub f { $MyApp::OneTime{+__FILE__.' '.__LINE__} ||= (print("logging\n"), 1 +); return(); } 1;
      I don't know what the + in front of the __FILE__ is supposed to do

      In the current form, it has no meaning. It is a leftover from an earlier version, which read something like:

      $MyApp::once{+__LINE__} ....
      and so the + stayed there and when posting my problem, I copied it without much thinking. If you have only __LINE__ as key, the + is needed because __LINE__ is a bareword.

      -- 
      Ronald Fischer <ynnor@mm.st>