Problem accessing fully qualified global var from another package

  • Thread starter Sven-Thorsten Fahrbach
  • Start date
S

Sven-Thorsten Fahrbach

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
 
P

Paul Lalli

Sven-Thorsten Fahrbach said:
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
 
S

Sven-Thorsten Fahrbach

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

I fear posting an executable version of the script would contain several hundred lines of code, so that probably is out of the question. I can, however, try to provide you with as much information as possible.
The modules that are interesting for the problem are

1. Config.pm
2. Log4Logalizer.pm

Here is the entire Log4Logalizer.pm:

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", ": $!";
}

# debug
carp "\$Logalizer::Config::logDirectory: ", "$Logalizer::Config::logDirectory\n";
carp "\$logName: $logName\n";

sysopen ($LOG, $logName, O_WRONLY | O_CREAT)
or croak "Can't open $logName: $!";
}

sub log {
my $message = shift;
my $verboseThreshold = shift || 0;

# try to open the file for writing and to get an exclusive lock
flock ($LOG, LOCK_EX | LOCK_NB) or croak "Can't lock $logName: $!";

if ($verboseThreshold >= $verboseLevel) {
print $LOG "$message\n";
}
}

1;

Here's how the global vars are declared in Config.pm:

#-----------------------------------------------------------------------# configuration variables, shared with other modules
#-----------------------------------------------------------------------our $scriptUrl;
our $outputDirectory;
our $outputUrl;
our $templateDirectory;
our $dataDirectory;
our $logfilePath;
our $backupLogfilePath;
our $baseUrl;
our $logDirectory; # for Log4Logalizer.pm

[...]

Config.pm has some subroutines to parse the configuration files. That works correctly, in fact everything is working correctly except the assignment in Log4Logalizer.pm.
Here are the error messages:

$Logalizer::Config::logDirectory: /srv/www/htdocs/logalizer
at /srv/www/cgi-bin/logalizer/src/logalizer.cgi line 83
$logName: /logalizer.log
at /srv/www/cgi-bin/logalizer/src/logalizer.cgi line 83
Can't open /logalizer.log: Permission denied at /srv/www/cgi-bin/logalizer/src/logalizer.cgi line 83
[Sat Jul 30 20:11:18 2005] [error] [client 127.0.0.1] Premature end of script headers: /srv/www/cgi-bin/logalizer/src/logalizer.cgi

I hope the code posted above is sufficient. I'd need to post the entire script to give you a more coherent image.

Thanks

SveTho
 
S

Sven-Thorsten Fahrbach

Okay, I was able to figure it out. The variable is, at the time of the assignment, not yet initialized. I've pasted the assignment into the init() sub and it's working fine now.

SveTho

p.s.: this is an example for why you shouldn't turn off warnings by default. Had I left out the

no warnings qw/ uninitialized /;

perl would have told me at once that it's not initialized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top