Hi, monks! I already did my homework by looking at the global variable discussion in this website and I also perused countless other webpages with no success. I want to commit the gravest sin of all and export a global variable from a package. The reason why I do it is so I can turn on the debug switch by changing the value of the variable in one module and then get all the assertions and print statements working in myriad of scripts and modules of this project I am working on. So, I tried this, in ActivePerl 5.10.0 build 1004:

package ConfigThisJunk; use strict; use warnings; BEGIN { use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( $DEBUG ); } our $DEBUG = 1; 1;
SomeJunk.pl use strict; use warnings; our $DEBUG; use ConfigThisJunk qw ( $DEBUG ); print $DEBUG;

and I got 'Use of uninitialized value $DEBUG in print'. If I however replace the last line by:

print $ConfigThisJunk::DEBUG;

it works. I suppose I could remove 'use strict', which I prefer not to or initialize $DEBUG lexically as

our $DEBUG = $ConfigThisJunk::DEBUG;

but I'd really like to understand what I'm doing wrong. Any help will be greatly appreciated. Many thanks.


In reply to Global variables in Perl by taioba

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.