Problem accessing fully qualified global var from another package

Discussion in 'Perl Misc' started by Sven-Thorsten Fahrbach, Jul 30, 2005.

  1. 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
     
    Sven-Thorsten Fahrbach, Jul 30, 2005
    #1
    1. Advertising

  2. Sven-Thorsten Fahrbach

    Paul Lalli Guest

    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
     
    Paul Lalli, Jul 30, 2005
    #2
    1. Advertising

  3. Re: Problem accessing fully qualified global var from anotherpackage


    > 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
     
    Sven-Thorsten Fahrbach, Jul 30, 2005
    #3
  4. Re: Problem accessing fully qualified global var from anotherpackage

    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.
     
    Sven-Thorsten Fahrbach, Jul 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Drebin

    Re: fully qualified domain name

    Frank Drebin, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    573
    Frank Drebin
    Aug 26, 2003
  2. Jacob Crossley
    Replies:
    0
    Views:
    406
    Jacob Crossley
    Apr 2, 2004
  3. patrimith
    Replies:
    2
    Views:
    723
    Goldfish
    Oct 11, 2007
  4. Szabolcs Borsanyi

    Pointer to qualified poitner to qualified object

    Szabolcs Borsanyi, May 30, 2008, in forum: C Programming
    Replies:
    13
    Views:
    562
    Keith Thompson
    Jun 8, 2008
  5. Tassilo v. Parseval

    'by package' sorting of fully qualified functions

    Tassilo v. Parseval, Jan 29, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    96
    Bo Lindbergh
    Jan 29, 2004
Loading...

Share This Page