In one of my modules I have a statement like this:

$MyApp::once{x} ||= 1;
Perl warns Name "MyApp::once" used only once: possibly typo. While it is true that this happened to be the only usage of %Pck::v in my program at the time, I wonder what is the best way to suppress this warning. Clearly I can't use use vars nor our here. Sure I can suppress the warnings with no warnings in the surrounding block. Any other suggestion?

OK, maybe we have a XY problem here, so this is how I stumbled into this problem (maybe you have a better suggestion, but I still would be interested in an answer to my original question as well): I have some functions, which gets executed often, but are supposed to log some information only during the first time they are executed:

sub f { ... log('some information'); # but should be called only the first time. ... }
In languages such as C++, I would use a local static for this purpose:
static LogRetType const once=log('some information');
We don't have local statics in Perl, so one possibility would be to use a 'my' variable on file scope:
my $once1=0; sub f { ... $once1 ||= (log('some information'),1); ... }
But I neither like the fact that the definition of $once1 is not logically close to its (only) usage, but also that I need to invent a new variable every time I do this in my package. So I thought that I would instead have a single variable of this kind for my whole application, but use a hash for this purpose, and the file name and line number as a key:
sub f { ... $MyApp{+__FILE__.' '.__LINE__} ||= (log('some information'),1); ... }
This works, as long as I have two or more lines of this type in my package; but if there is only one occurence, I get the aforementioned warning.

-- 
Ronald Fischer <ynnor@mm.st>

In reply to Why do I get a "used only once" warning here? by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.