Hi all,

I've been coding Perl for 12 years. I wouldn't call myself a Perl coder, but I've written several large applications in the language. Last week, I heard, for the first time, that it's good practice to "use strict" and decided to try this out by bringing one of said applications into compliance. (Yeah, I'm Johnny-come-lately.)

Turned out to be not too bad, since my code was mostly fine in that regard (all variables within subroutines were declared with "my"), except for one small, but substantial problems. For ease of coding, I've separated subroutines into several files that I load into the main script using require. The main script is merely a collection of calls to the various subroutines. Now, I have some global variables that I define in the main script, and that are used throughout the sub-scripts. Now that I'm using the strict pragma, these variables are scoped to just the main script, and are empty in the subroutines.

Now the easiest solution would be to combine all of the files so as to eliminate the problem, but I feel like that can't (or at least shouldn't) be a "best practice". I guess I can define the variables within each of the files, but ditto. I've read a bunch of stuff regarding exporting variables, but I'm calling the other scripts using "require" not "use", and don't really understand why and if I should be using "use". I've also come across importing variables from a file that you're calling with require, but I have the opposite problem.

Am I missing something really obvious? I don't get it! Sorry about how muddled this question is, I'm just rather confused.


In reply to Using variables in require file... not possible? by tunafish

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.