taioba has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Global variables in Perl
by ikegami (Patriarch) on Jun 03, 2010 at 17:48 UTC | |
by taioba (Acolyte) on Jun 03, 2010 at 19:07 UTC | |
by ikegami (Patriarch) on Jun 03, 2010 at 19:10 UTC | |
|
Re: Global variables in Perl
by LanX (Saint) on Jun 03, 2010 at 17:17 UTC | |
|
Re: Global variables in Perl
by ikegami (Patriarch) on Jun 03, 2010 at 19:15 UTC | |
by taioba (Acolyte) on Jun 04, 2010 at 16:33 UTC | |
by ikegami (Patriarch) on Jun 04, 2010 at 17:43 UTC | |
by taioba (Acolyte) on Jun 04, 2010 at 20:11 UTC | |
by ikegami (Patriarch) on Jun 04, 2010 at 20:18 UTC | |
| |
|
Re: Global variables in Perl
by rovf (Priest) on Jun 04, 2010 at 09:43 UTC |