Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Passing filename to Log::Std

by borisz (Canon)
on Apr 12, 2006 at 20:04 UTC ( [id://542938]=note: print w/replies, xml ) Need Help??


in reply to Passing filename to Log::Std

The module is working fine. You just initialize the var $filename to late. use implies a BEGIN block. So your $filename is undef unless you initialize it also in a BEGIN block.
our $filename; BEGIN { $filename = '/tmp/mylogfile'; } use Log::StdLog { file => $filename }; print {*STDLOG} warn => 'Works fine!!!';
or call import yourself.
my $filename = '/tmp/mylogfile'; require Log::StdLog; Log::StdLog->import({ file => $filename }); print {*STDLOG} warn => 'Works fine!!!';
Boris

Replies are listed 'Best First'.
Re^2: Passing filename to Log::Std
by bowei_99 (Friar) on Apr 12, 2006 at 21:40 UTC
    OK, that makes sense. However, I'm trying to set $file and $level according to what's passed into the subroutine. But, when I run my code below, it still complains of an uninitialized value(s)...

    sub StartLogging { our $ref = shift @_; # ref to config hash our ($file, $level); BEGIN { $file = $ref->{log}{file_base} . ".$ref->{hostname}{target}.$ +0.$$.log"; $level = $ref->{log}{level}; } if ( $ref->{log}{method} eq "StdLog" ) { print "Logging to file $file at minimum loglevel $level\n"; use Log::StdLog { level => $level, file => $file, }; #now, log messages can be written to {*STDLOG} my $print_rc = print {*STDLOG} warn => "warn - this is a test" +; } }

    -- Burvil

      Use require and import(), not use if you're going to do it that way. As soon as the Perl parser finishes parsing the use statement, it runs it. This happens before StartLogging even finishes compiling, and much before anything calls the subroutine.

      your code just move the problem. Try this: (untested)
      sub StartLogging { my $ref = shift @_; # ref to config hash my ( $file, $level ); $file = $ref->{log}{file_base} . ".$ref->{hostname}{target}.$0.$$.l +og"; $level = $ref->{log}{level}; if ( $ref->{log}{method} eq "StdLog" ) { print "Logging to file $file at minimum loglevel $level\n"; require Log::StdLog; Log::StdLog->import( { level => $level, file => $file, } ); #now, log messages can be written to {*STDLOG} print {*STDLOG} warn => "warn - this is a test"; } }
      Boris
        Hm, odd. That code crashes perl on win32, and so I think it would do the same on linux (segfault).

        Update: fixed typo, now runs with error messages:

        H:\working_files\win32>perl logtest.pl Use of uninitialized value in concatenation (.) or string at logtest.p +l line 32. Logging to file H:\working_files\test\testfile..logtest.pl.2096.log at + minimum l oglevel 'info' Unable to open log file 'H:\working_files\test\testfile..logtest.pl.20 +96.log' at logtest.pl line 46
        so, we can see that the variables in the config hash aren't being read in correctly....

        -- Burvil

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://542938]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-28 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found