Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Problems accessing fully qualified var from another module

by svetho (Beadle)
on Jul 30, 2005 at 20:01 UTC ( #479642=perlquestion: print w/replies, xml ) Need Help??

svetho has asked for the wisdom of the Perl Monks concerning the following question:

My dear monks

I'm really busting my brains over the following problem:

I've got a script with several modules, including one that reads in data from a configuration file. The module then stores the retrieved data in global variables, e.g. one that is declared with 'our $dataDirectory'. My log module needs to retrieve a path where to store its log file in, which is also retrieved by the Config.pm module. Here is the code:

package Logalizer::Log4Logalizer; use strict; use warnings; no warnings qw/ uninitialized /; use Carp; use Fcntl qw/ :DEFAULT :flock /; use Logalizer::Config; BEGIN { use Exporter(); our ($VERSION, @ISA, @EXPORT); $VERSION = 1.00; @ISA = qw/ Exporter /; @EXPORT = qw/ $verboseLevel &init &log /; } our $verboseLevel = 0; our $logName = "$Logalizer::Config::logDirectory/logalizer.log"; our $LOG = \*LOG; sub init { unless (-d $Logalizer::Config::logDirectory) { mkdir $Logalizer::Config::logDirectory, 0777 or croak "Can't mkdir $Logalizer::Config::logDirectory", ": $!"; } sysopen ($LOG, $logName, O_WRONLY | O_CREAT) or croak "Can't open $logName: $!"; }

...

I monitor $Logalizer::Config::logDirectory with carp()s and I get the correct path, let's say '/path/to/logfile'. I also monitor $logName, what I get, however, is '/logalizer.log', i.e. $Logalizer::Config::logDirectory doesn't seem to be interpolated in the above assignment. What's even more amazing is that the very same kind of assignment works in all the other modules. It's probably something obvious but I'm really at a loss here. Does anybody know what's going wrong and what I can do about it?

Any help will be greatly appreciated.

SveTho

Replies are listed 'Best First'.
Re: Problems accessing fully qualified var from another module
by saintmike (Vicar) on Jul 30, 2005 at 20:14 UTC
    Getting rid of
    no warnings qw/ uninitialized /;
    will show you that $Logalizer::Config::logDirectory isn't initialized by the time you're using it in the our variable assignment.

      Oh man, thanks. Another example for why you shouldn't turn off warnings by default. I pasted the assignment into the init() sub and it's working fine now.

      Thanks again

      SveTho

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://479642]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2020-10-21 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (212 votes). Check out past polls.

    Notices?