Rob Dixon wrote:
rafailowski wrote:
I have a problem with Getopt::Long and Log::StdLog.

An example script, i always have the following error :
Use of uninitialized value in hash element at /usr/local/share/perl/5.10.0/Log/StdLog.pm line 57

level => $cmd_args_ref->{"log_level"} is always undef(???) but
print $cmd_args_ref->{"log_level"}; return the good value

I try to fix this with BEGIN block (around GetOptions) with no success

Is there a way to solve this (BEGIN INIT block?)

#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Long;

my %cmd_args = ();
my $cmd_args_ref = \%cmd_args;

GetOptions(
    "log-level=s"   =>  \$cmd_args{"log_level"},
);

sub log_format {
    my ($date, $pid, $level, @message) = @_;
    return "[$date][$level]: " . join(q{}, @message);
};

use Log::StdLog {
    format => \&log_format,
    file => "$0.log",
    level => $cmd_args_ref->{"log_level"},
};

print $cmd_args_ref->{"log_level"};

Calling GetOptions in a BEGIN block will work fine, but the initialisation of
$cmd_args_ref must be done at the same time, otherwise it won't be set up when
Log::StdLog comes to be imported. The code below should do what you want.

HTH,

Rob



use strict;
use warnings;

use Getopt::Long;

my (%cmd_args, $cmd_args_ref);

BEGIN {
  GetOptions(
    'log-level=s' => \$cmd_args{log_level},
  );
  $cmd_args_ref = \%cmd_args;
}

sub log_format {
  my ($date, $pid, $level, @message) = @_;
  local $" = q{};
  return "[$date][$level]: @message";
}

use Log::StdLog {
  format => \&log_format,
  file => "$0.log",
  level => $cmd_args_ref->{log_level},
};

print STDLOG user => "HELLO\n";
Ohhh, it works fine!
thanks for your help...
rafailow

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to