Perl: library to log on specific files -
Perl: library to log on specific files -
i'm creating library stuffs want log errors on specific file. unfortunately, while works if initiate 1 single instance of library, doesn't if initiate more 1 instance.
the results in case output logged in lastly file , not half , half expecting.
this main.pl
eval 'exec /usr/bin/perl -i `pwd` -s $0 ${1+"$@"}' if 0; utilize strict; utilize mylibrary; ($rc, $test_2, $test_1); # output not going file exit $test_1 if (($test_1 = mylibrary->new("/tmp", "test_1")) !~ "hash"); # going file exit $test_2 if (($test_2 = mylibrary->new("/tmp", "test_2")) !~ "hash"); exit $rc if ( $rc = $test_1->test() ); exit $rc if ( $rc = $test_2->test() );
and mylibrary.pm
package mylibrary; utilize strict; utilize symbol; utilize vars qw($version @isa @export %default); @export = qw( ); $version = '1.00'; require 5.000; %default; $fh; sub new { $rc; ($proto, $log_dir, $log_file) = @_; $class = ref($proto) || $proto; $self = { %default }; bless($self, $class); $fh = gensym; ($self->{'log_dir'}, $self->{'log_file'}) = ($log_dir, $log_file); homecoming $rc if ( $rc = $self->open_log_file() ); homecoming $self; } sub destroy { $rc; $self = shift; homecoming $rc if ( $rc = $self->close_log_file() ); } sub open_log_file { $self = shift; open $fh, ">>$self->{'log_dir'}/$self->{'log_file'}" or die "cannot open file $self->{'log_dir'}/$self->{'log_file'}"; homecoming 0; } sub close_log_file { $self = shift; close($fh) or die "cannot close $self->{'log_dir'}/$self->{'log_file'}"; homecoming 0; } sub test { $self = shift; print $fh "[$self->{'log_file'}]\n"; homecoming 0; } 1;
one more thing ... in example, i'm using $fh
global variable, while have variable part of %default
hash. however, if seek create part of hash replacing $fh
occurences $self->{'fh'}
, next error:
string found operator expected @ mylibrary.pm line 75, near "} "[$self->{'log_file'}]\n"" (missing operator before "[$self->{'log_file'}]\n"?) syntax error @ mylibrary.pm line 75, near "} "[$self->{'log_file'}]\n""
row 75 in case following:
sub test { $self = shift; row 75 =>>> print $self->{'fh'} "[$self->{'log_file'}]\n"; homecoming 0; }
while total library reviewed accordingly is:
package mylibrary; utilize strict; utilize symbol; utilize vars qw($version @isa @export %default); @export = qw( ); $version = '1.00'; require 5.000; %default; sub new { $rc; ($proto, $log_dir, $log_file) = @_; $class = ref($proto) || $proto; $self = { %default }; bless($self, $class); $self->{'fh'} = gensym; ($self->{'log_dir'}, $self->{'log_file'}) = ($log_dir, $log_file); homecoming $rc if ( $rc = $self->open_log_file() ); homecoming $self; } sub destroy { $rc; $self = shift; homecoming $rc if ( $rc = $self->close_log_file() ); } sub open_log_file { $self = shift; open $self->{'fh'}, ">>$self->{'log_dir'}/$self->{'log_file'}" or die "cannot open file $self->{'log_dir'}/$self->{'log_file'}"; homecoming 0; } sub close_log_file { $self = shift; close($self->{'fh'}) or die "cannot close $self->{'log_dir'}/$self->{'log_file'}"; homecoming 0; } sub test { $self = shift; print $self->{'fh'} "[$self->{'log_file'}]\n"; homecoming 0; } 1;
empirically, seems file handle in print
statement cannot arbitrary expression. minor modification of code, mylibrary.pm
compile, replaced:
print $self->{'fh'} "[$self->{'log_file'}]\n";
with:
my $fh = $self->{'fh'}; print $fh "[$self->{'log_file'}]\n";
there other minor tweaks, code worked me:
mylibrary.pmpackage mylibrary; utilize warnings; utilize strict; utilize vars qw($version @isa @export %default); @export = qw(); $version = '1.00'; require 5.000; sub new { ($proto, $log_dir, $log_file) = @_; $class = ref($proto) || $proto; $self = { %default }; bless($self, $class); $self->{'log_dir'} = $log_dir; $self->{'log_file'} = $log_file; $self->open_log_file(); homecoming $self; } sub destroy { $rc; $self = shift; homecoming $rc if ( $rc = $self->close_log_file() ); } sub open_log_file { $self = shift; $log_file = "$self->{log_dir}/$self->{log_file}"; open $self->{'fh'}, ">>", $log_file or die "cannot open file $log_file"; return; } sub close_log_file { $self = shift; close($self->{'fh'}) or die "cannot close $self->{'log_dir'}/$self->{'log_file'}"; return; } sub print_data { $self = shift; $fh = $self->{fh}; print $fh @_, "\n"; } sub test { $self = shift; $fh = $self->{'fh'}; print $fh "[$self->{'log_file'}]\n"; homecoming 0; } 1;
i'm not convinced use 5.000;
buys much. chances of finding perl 4.x still running pretty remote. these days, before perl 5.8 long dead (or, if isn't, should be).
there many minor improvements made in code not shown above.
testcase.pl#!/usr/bin/env perl utilize warnings; utilize strict; utilize mylibrary; ($rc, $test_2, $test_1); $counter = 0; sub counter { printf"ok %d\n", ++$counter; } counter; # output not going file exit $test_1 if (($test_1 = mylibrary->new("/tmp", "test_1")) !~ "hash"); counter; # going file exit $test_2 if (($test_2 = mylibrary->new("/tmp", "test_2")) !~ "hash"); counter; exit $rc if ( $rc = $test_1->test() ); counter; exit $rc if ( $rc = $test_2->test() ); counter; $test_1->print_data("extra information"); $test_2->print_data("missing syncopation"); print "finished\n";
nth sample run it looks ran previous edition of testcase.pl
once, before adding print_data
function, , 4 times since adding print_data
function.
$ perl -i$pwd testcase.pl ok 1 ok 2 ok 3 ok 4 ok 5 finished $ cat /tmp/test_1 [test_1] [test_1] info [test_1] info [test_1] info [test_1] info $ cat /tmp/test_2 [test_2] [test_2] missing syncopation [test_2] missing syncopation [test_2] missing syncopation [test_2] missing syncopation $
perl
Comments
Post a Comment