in reply to do, local and a qualified identifier?

> what is the value of $Foo::bar in the main script and why?

42. Because Foo::init() sets the package variable explicitly.

As for the local(), that has no effect on the scope inside of the init.

Is there something else happening in the larger context (or maybe that's a remnant of the boilerplate config where it is sometimes required?) It's probably the latter — an onion in the varnish.

Qualifying with the package name? As opposed to setting the value of any random $bar? If you didn't declare it with our(), it won't work under strict.

  • Comment on Re: do, local and a qualified identifier?

Replies are listed 'Best First'.
Re^2: do, local and a qualified identifier?
by ikegami (Patriarch) on Oct 09, 2005 at 16:42 UTC
    Whether init sets the variable explicitely or not has no bearing on the answer.
    Foo.pm ------ use strict; use warnings; sub init { print("Setting \$Foo::bar to 42.\n"); $Foo::bar = 42; } local $Foo::bar; init(); 1;
    main.pl ------- use strict; use warnings; use Foo (); print("\$Foo::bar = $Foo::bar\n");
    output ------ Setting $Foo::bar to 42. Use of uninitialized value in concatenation (.) or string at main.pl l +ine 7. $Foo::bar =
      >>Later on init gets called from the main script.
      >>After that, what is the value of $Foo::bar in the main script and why?
      
      >...explicitly...no bearing on the answer

      It does, just that the wrong question was asked. Note that your response (bullet point 4) compiles into the same neuroncode as mine.

      Your new example has reframed the problem into use() of a module, where the original question was about do() and it also also calls init() not from the main script, but from the module (where the local($Foo::bar) happens to be in effect.) The OP states that init() gets called from the main script. True, if the file getting do()ne calls init(), something similar to what you demo will happen.

      The point that I was trying to make is that the local() had absolutely no impact on what happens inside of init(), so the value of $Foo::bar follows the same rules that it would if the local() statement never existed.

      If the OP's actual code is truly like the sample, then this is simply a case of assuming that an artifact of accident or ignorance is is somehow attributable to design. I opted not to reimplement the documentation of do, our, local, perlsyn, and perlvar under the assumption that the OP was in possesion of said fine documentation. In this case, the simple answer that it is probably an onion in the varnish (while that varnish may, as ysth suggests, belong to perl) seems sufficient to inspire the OP with the required curiosity.