Hi monks, I'm having this problem that I find very confusing, but which (I'm certain) has an easy answer which I can't find specifically addressed in the Q&A section or elsewhere.

Basically I have a perl library (.pl) file. In it I declare a global variable our $var to be used in other modules via require etc. I also use strict; and use warnings;. In my perl module (.pm) this warning appears when I start using the 'global' variable: Global symbol "$var" requires explicit package name...

When I remove the use strict this error does not appear, but I suspect that the problem remains. Am I right in believing the $var variable is being re-declared in the perl module or has it become local to the perl module file, and the declaration our $var in the perl library file is ignored?

All I want to do is use the straight $var in other files and for $var to act like a true global variable.

simple code mock-up:

#-----file1.pl----- use strict; use warnings; our $var = 1; #------------------ #-----file2.pm----- use strict; use warnings; require 'file1.pl'; $var = 2; # <-- This doesn't work, but it is supposed to be global, + right? #------------------

In reply to strict/warnings interference with global declarations by v_o_i_d

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.