For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > Problem accessing fully qualified global var from another package









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem accessing fully qualified global var from another package
Sven-Thorsten Fahrbach

2005-07-30, 5:00 pm

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 t
he 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
Paul Lalli

2005-07-30, 5:00 pm

Sven-Thorsten Fahrbach wrote:
> 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'.


Where? I don't see any carp()s in the above code. At what point in
the code are you confirming that this variable has the value you expect
it to? That is a necessary piece of information.

> 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.


Again, where?

> Does anybody know what's going wrong and what I can do about it?


We can't answer that with any certainty with the information provided.
My *guess* is that you are confirming the correctness of the variable
at some point in the code that is executed after the $logName
assignment is made.

Please post a short, but *complete*, program, that we can run by copy
and pasting, which demonstrates your error.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com