CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Another Log::Message problem - Can't call method "croak" on an undefined value

 



ohaya
New User

Apr 11, 2018, 5:09 PM

Post #1 of 3 (3248 views)
Another Log::Message problem - Can't call method "croak" on an undefined value Can't Post

Here's the whole program:


Code
#!C:\Perl64\bin\perl.exe 
use strict;
use warnings;

use Log::Message private => 0, config => 'e:\Ziptemp\cf_file';

my $log = Log::Message->new( private => 1,
level => 'log',
config => 'e:\Ziptemp\cf_file',
);

$log->store('this is my first message');

$log->store( message => 'message #2',
tag => 'MY_TAG',
level => 'carp',
extra => ['this is an argument to the handler'],
);

my @last_five_items = $log->retrieve(5);

my @items = $log->retrieve( tag => qr/my_tag/i,
message => qr/\d/,
remove => 1,
);

@items = $log->final( level => qr/carp/, amount => 2 );

my $first_error = $log->first();

# croak with the last error on the stack
$log->final->croak;

# empty the stack
$log->flush();


Code
 
When I run it I get:

message #2 at C:/Perl64/site/lib/Log/Message.pm line 409. at Wed Apr 11 20:02:59 2018
Can't call method "croak" on an undefined value at logtest.pl line 32.


Also, I have an off-topic question: Why are there two config files that are needed? I set them both to the same file for now but I don't understand why it would need to different configuration files?

Thanks,
Jim


Zhris
Enthusiast

Apr 11, 2018, 5:34 PM

Post #2 of 3 (3244 views)
Re: [ohaya] Another Log::Message problem - Can't call method "croak" on an undefined value [In reply to] Can't Post

Hi,

If you have defined "remove = 1" in your config, then Log::Message's retrieve method ( called by methods such as final ) will consume messages. When you call final at line 32, the messages have already been consumed by prior retrieves, the stack is empty, and it returns undefined. The chain to croak is effectively "do { undef }->croak", triggering the error you report.

There are various ways this could be managed when remove = 1 i.e. checking if defined, retrieving all into a list etc. Otherwise set the remove config attibute to 0.

Chris


Zhris
Enthusiast

Apr 11, 2018, 5:44 PM

Post #3 of 3 (3243 views)
Re: [ohaya] Another Log::Message problem - Can't call method "croak" on an undefined value [In reply to] Can't Post


Quote
Also, I have an off-topic question: Why are there two config files that are needed? I set them both to the same file for now but I don't understand why it would need to different configuration files?


Providing config attributes on use sets the default attributes on new. This is useful if you are creating more than one Log::Message object which share some of the same attributes, but not others. I personally don't like the use / import interface, and would just provide all of the attributes on new. it provides clarity and doesn't cause issues later if multiple instances across multiple scripts ( default config is stored in a global package variable! ).


Code
use Log::Message level => 'log'; 
my $one = Log::Message->new( config => 'one' );
my $two = Log::Message->new( config => 'two' );

# is equal to:

use Log::Message;
my $one = Log::Message->new( level => 'log', config => 'one' );
my $two = Log::Message->new( level => 'log', config => 'two' );


Chris


(This post was edited by Zhris on Apr 11, 2018, 5:52 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives