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

Clearly I can't use use vars nor our here.
I don't get this point. Why can't they be used? Either one seems to do the job without the need to turn off warnings, although with perhaps a bit more verbosity (but also less worry about proper scoping).
>perl -wMstrict -le "package L; use vars qw(%lone); our %once; sub l { return print __PACKAGE__, qq{: @_} } package main; print 'do some stuff in ', __PACKAGE__; package L; use vars qw(%singular); our %onetime; package main; $L::once{x} ||= (L::l('hi from', __PACKAGE__), 1); $L::lone{x} ||= (L::l('hello from', __PACKAGE__), 1); $L::onetime{x} ||= (L::l('hiya from', __PACKAGE__), 1); $L::singular{x} ||= (L::l('greets from', __PACKAGE__), 1); $L::once{foo} ||= L::l('this should print'); $L::once{foo} ||= L::l('but this should NOT print!'); " do some stuff in main L: hi from main L: hello from main L: hiya from main L: greets from main L: this should print
Update: Replaced rather verbose example with much more succinct one.

Replies are listed 'Best First'.
Re^2: Why do I get a "used only once" warning here?
by rovf (Priest) on Mar 16, 2009 at 08:37 UTC

    Indeed, the way you wrote it, it would work. In my original design, the variable would be in a different package, so neither

    use vars qw(%MyApp::once);
    nor
    our %MyApp::once;
    would have worked, because package qualifications are not allowed in use vars and our; put actually I don't need to put once in its own package; I can simply have it in my current package, and this would make your solution indeed feasible.

    -- 
    Ronald Fischer <ynnor@mm.st>
      ... the variable would be in a different package ...
      ... I don't need to put  once in its own package; I can simply have it in my current package, and this would make your solution indeed feasible.
      I'm still a bit confused, and I suspect we may be posting at cross-purposes.

      What I wanted to illustrate is that a package variable may be declared in any package at any time (with either use vars or our), and may then be accessed from any other package at any (executionally subsequent) time.

      >perl -wMstrict -le "package Some::Package; our $scalar = 'foo'; package Some::Other::Package; print $Some::Package::scalar; print 'current package is ', eval { scalar caller }; " foo current package is Some::Other::Package
      Update: Oops... Forgot to include example code.
        What I wanted to illustrate is that a package variable may be declared in any package at any time (with either use vars or our), and may then be accessed from any other package at any (executionally subsequent) time.

        I see. I had misunderstood you here. So I would declare it for instance in the logging package. Makes sense.

        -- 
        Ronald Fischer <ynnor@mm.st>