Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem accessing fully qualified global var from another package

Reply
Thread Tools

Problem accessing fully qualified global var from another package

 
 
Sven-Thorsten Fahrbach
Guest
Posts: n/a
 
      07-30-2005
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/ EFAULT :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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-30-2005
Sven-Thorsten Fahrbach wrote:
> package Logalizer::Log4Logalizer;
>
> use strict;
> use warnings;
> no warnings qw/ uninitialized /;
> use Carp;
> use Fcntl qw/ EFAULT :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

 
Reply With Quote
 
 
 
 
Sven-Thorsten Fahrbach
Guest
Posts: n/a
 
      07-30-2005

> 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/ EFAULT :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
 
Reply With Quote
 
Sven-Thorsten Fahrbach
Guest
Posts: n/a
 
      07-30-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to qualified poitner to qualified object Szabolcs Borsanyi C Programming 13 06-08-2008 07:52 PM
determining fully qualified package & class name patrimith Python 2 10-11-2007 08:08 PM
'by package' sorting of fully qualified functions Tassilo v. Parseval Perl Misc 5 01-29-2004 05:27 PM
Re: fully qualified domain name Frank Drebin ASP .Net 0 08-26-2003 08:43 PM



Advertisments