I have been handed a Perl script to work with that has a configuration file specified by a command-line argument.

The configuration file is selected based on the command-line parameter and then loaded with a do $config_filename in the main script.

In a given configuration file it says something along the lines of:

package Foo; use strict; use warnings; local ($Foo::bar); sub init { $Foo::bar = 42; }

Later on init gets called from the main script.

After that, what is the value of $Foo::bar in the main script and why?

What is the purpose of the declaring $Foo::bar local? I thought it gave it a global symbol a local value?

If so, when init is called will it set the localized value like a closure or the global value?

Further, what is the point of qualifying the identifier with the package name? and does that have any effect on the functionality?

Totally confused. Have not seen this way to do it before.

Update: Yes, we were getting the "$bar used only once" error when I changed it to use our and unqualified - even though bar is mentioned outside of the do. This is what confused me. Why do you get that error for an our and not for a local? I don't see it changing the number of times it is mentioned. (In fact, couldn't perl warn "useless use of local" in the above code, as the local does absolutely nothing.)

-Andrew.


In reply to do, local and a qualified identifier? by tomazos

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.