Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

The text of the tutorial doesn't mention the fact that the scope of the effect of use vars is file-level. This is, however, documented in the perldoc for vars:

the "use vars" and "use subs" declarations are not BLOCK-scoped. They are thus effective for the entire file in which they appear.
(it's also mentioned under "Pragmatic Modules" in the perlmodlib man page).

Even though its "maximum reach" is one file, the effects of use vars are still different than those of our, which was all I was trying to document in the tutorial.

So, the general answer to your question is that you can't do exactly what you say you want to do, and that's one of the points of use strict: if you're going to use a variable in a file, you have to declare it explicitly in that file; it encourages you to know where everything's coming from, and protects you from typos in variable names.

This doesn't mean your problem's insoluble, though. If, you want inc.pl to serve the function of being a "configuration script" that initializes a bunch of values so other scripts can use them, you can have it work this way: create one data structure (say, a hash), and 'export' that hash. This way, a script that uses inc.pl will only need to declare one variable with our or use vars; of course, with a hash, you lose the safety mentioned as one of the chief benefits of use strict.

For this reason, instead of using a simple hash, you might want to have inc.pl be an object oriented module, but that's beyond the scope of this tutorial.

HTH!

If not P, what? Q maybe?
"Sidney Morgenbesser"


In reply to Re^2: Variable Scoping in Perl: the basics by arturo
in thread Variable Scoping in Perl: the basics by arturo

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2014-07-23 00:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (130 votes), past polls