use strict; use warnings; BEGIN { use Fcntl qw( :flock SEEK_END ); sub lock { my ($fh) = @_; flock($fh, LOCK_EX) or die "Unable to obtain lock: $!\n"; # and, in case someone appended while we were waiting... seek($fh, 0, SEEK_END) or die "Unable to seek to EOF: $!\n"; } sub unlock { my ($fh) = @_; flock($fh, LOCK_UN) or die "Unable to unlock: $!\n"; } *CORE::GLOBAL::open = sub (*;$@) { use Symbol (); my $handle = Symbol::qualify_to_ref($_[0], scalar caller); $_[0] = $handle unless defined $_[0]; # pass up to caller my $result; if (@_ == 1) { $result = CORE::open $handle; } elsif (@_ == 2) { $result = CORE::open $handle, $_[1]; } elsif (@_ == 3) { if (defined $_[2]) { $result = CORE::open $handle, $_[1], $_[2]; } else { $result = CORE::open $handle, $_[1], undef; # special case } } else { $result = CORE::open $handle, $_[1], $_[2], @_[3..$#_]; } lock $handle if defined $result; # CORE::open returns undef on failure. # It could legitimately return '0' on # success. See 'Fatal' for details. return $result; }; *CORE::GLOBAL::close = sub (;*) { unlock( $_[0] ); return close ( $_[0] ); }; }