TStanley has asked for the wisdom of the Perl Monks concerning the following question:
package Logger::Simple; use strict; use Carp; use FileHandle; use Fcntl qw(:flock); use vars qw /$VERSION $SEM/; $VERSION='1.06'; $SEM = ".LS.lock"; sub new{ my($class,%args)=@_; my $self = bless{LOG => $args{LOG} || croak"No logfile!\n", CARP => $args{CARP} || undef, FILEHANDLE => new FileHandle || croak"Unable to ge +t filehandle\n", SEMAPHORE => new FileHandle || croak"Unable to cr +eate semaphore filehandle\n", ERROR => "", HISTORY => [], },$class; if(! $self->open()){ die"Unable to open $self->{LOG}\n"; } return $self; } sub open{ my $self=shift; if(! open($$self{FILEHANDLE},">>$$self{LOG}")){ $self->set("Unable to open logfile\n"); return 0; } $$self{FILEHANDLE}->autoflush(1); return 1; } sub write{ my($self,$msg)=@_; my $FH=*{$$self{FILEHANDLE}}; my $format="$0 : [".scalar (localtime)."] $msg"; $self->lock(); if(! print $FH "$format\n"){ $self->set("Unable to write to $$self{LOG}: $!\n"); } $self->unlock(); } sub message{ my $self=shift; if(wantarray){ my @messages=@{$$self{HISTORY}}; return @messages; }else{ my $message=$$self{ERROR}; return $message; } } sub clear{ my $self=shift; $$self{ERROR}=undef; } sub set{ my ($self,$error)=@_; $self->clear; $$self{ERROR}=$error; push @{$$self{HISTORY}},$error; carp "$error\n" if $$self{CARP}; $self->write($error); } sub print_object{ require Data::Dumper; my $self=shift; print Data::Dumper->Dumper($self); } sub lock{ my $self=shift; my $FH=*{$$self{SEMAPHORE}}; if(-e $SEM){ flock($FH,LOCK_NB) or carp"Can't obtain file lock: $!\n"; open($FH,">$SEM")||die"Can't create lock file: $!\n"; flock($FH,LOCK_EX) or die"Can't obtain file lock: $!\n"; }else{ open $FH,">$SEM"||die"Can't create lock file: $!\n"; flock($FH,LOCK_EX) or die"Can't obtain file lock: $!\n"; } } sub unlock{ my $self=shift; my $FH=*{$$self{SEMAPHORE}}; if(-e $SEM){ flock(S,LOCK_UN); close S; unlink $SEM; } } 1;
Here are the scripts in question:Ambiguous call resolved as CORE::open(), qualify as such or use & at / +opt/perl5/ lib/site_perl/5.6.0/Logger/Simple.pm line 88. Ambiguous call resolved as CORE::open(), qualify as such or use & at / +opt/perl5/ lib/site_perl/5.6.0/Logger/Simple.pm line 88. Ambiguous call resolved as CORE::open(), qualify as such or use & at / +opt/perl5/ lib/site_perl/5.6.0/Logger/Simple.pm line 91. Ambiguous call resolved as CORE::open(), qualify as such or use & at / +opt/perl5/ lib/site_perl/5.6.0/Logger/Simple.pm line 91. Use of uninitialized value in ref-to-glob cast at /opt/perl5/lib/site_ +perl/5.6.0 /Logger/Simple.pm line 85. Use of uninitialized value in ref-to-glob cast at /opt/perl5/lib/site_ +perl/5.6.0 /Logger/Simple.pm line 85. flock() on closed filehandle main:: at /opt/perl5/lib/site_perl/5.6.0/ +Logger/Sim ple.pm line 87. more lines follow ...
To note something however, the behavior of the scripts is the same as far as writing into the log file. How can I get rid of these warning messages (other than run without warnings)?#!/opt/perl5/bin/perl # locker.pl use strict; use Logger::Simple; my $L=Logger::Simple->new(LOG=>"file.txt",CARP=>'1'); for(1..10){ $L->write("Writing");} #!/opt/perl5/bin/perl -w # writer.pl use strict; use Logger::Simple; my $L=Logger::Simple->new(LOG=>"file.txt",CARP=>'1') for(1..5){ $L->write("Getting through?"); } #!/bin/sh # runner.sh ./locker.pl & ./writer.pl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File locking with semaphores
by jasonk (Parson) on Mar 07, 2003 at 19:00 UTC | |
|
Re: File locking with semaphores
by TStanley (Canon) on Mar 07, 2003 at 19:58 UTC | |
by runrig (Abbot) on Mar 07, 2003 at 20:26 UTC | |
|
Re: File locking with semaphores
by runrig (Abbot) on Mar 07, 2003 at 19:20 UTC |