Since you used the term "#include" I assume you're coming from a C or C++ background where the included file is inserted into the calling file by the pre-processer before the parser gets ahold of it. In those languages it's as if you had typed the second file inside the first so variables declared in the included file would be available in the parent.

In this part of Perl, the language works a bit more like Java and Modula 2. The packages you "require" or "use" have their own scoping which is applied when you use keywords like "my".

The "my" in test2.pl restricts the lexical scope of the $x variable to the enclosing block. In this case, the block is the file test2.pl so the variable $x can only be used inside that file and will not be able to be accessed elsewhere.

It looks like you have reduced a real life problem to the bare essense to elminate any non-contributing factors to studying the problem. In general, this is a great thing as it saves others a lot of time wading through code unrelated to the problem at hand. Unfortunately, it also makes it difficult for me to know what to recommend as a workaround to your problem as I don't know exactly how the data is being used and why you want to share it.

There are options of global variables, "our" variables, package variables with external access, returning values from a function, and probably other ideas folks could come up with. You're probably going to get some recommendations that you not share data at all, but sometimes it's a good thing. It's just difficult to provide an opinoin with the sterilized sample.

I can give you one other hint: The shabang line in test2.pl (#!/usr/bin/perl -w) is unnecessary and, in fact, is ignored when that file is "require"d. It is probably just misleading to the code reader unless test2.pl is actually run as a top level program itself.

There's no special first line needed for Perl packages which are used in a "require" or "use".

-- Eric Hammond


In reply to Re: the #include business by esh
in thread the #include business by Anonymous Monk

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.