require only parses a file if it hasn't already been required before. The first time vars.pl is required occurs in foo.pm in the package "foo" and so $bar= "I AM BAR." actually sets $foo::bar= "I AM BAR.". The second time vars.pl is required, Perl says, "Yes, you require that file and I've already loaded it so I don't need to do anything here". So $main::bar is never set.

If you want to set a variable in the caller's package every time your file is required, then I suggest you make the file a real module, like:

package MyVars; use base qw(Exporter); use vars qw(@EXPORT_OK $bar); @EXPORT_OK= qw($bar); $bar="I AM BAR."; 1;

Then you use it via use MyVars qw($bar);.

You can also include files via "do" to have them parsed every time, but I strongly frown on that.

        - tye (but my friends call me "Tye")

In reply to RE: require in .pm and .pl = blow up? by tye
in thread require in .pm and .pl = blow up? 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.