package Ubic::Lockf; { $Ubic::Lockf::VERSION = '1.57'; } use
strict; use warnings; # ABSTRACT: file locker with an automatic
out-of-scope unlocking mechanism use Fcntl qw(:flock); use
Params::Validate; use POSIX qw(:errno_h); use Carp; use
Ubic::Lockf::Alarm; use parent qw(Exporter); our @EXPORT =
qw(lockf); sub DESTROY ($) { my ($self) = @_; local $@; my $fh =
$self->{_fh}; return unless defined $fh; # already released or
dissolved flock $fh, LOCK_UN; delete $self->{_fh}; # closes the
file if opened by us } my %defaults = ( shared => 0, blocking =>
1, timeout => undef, mode => undef, ); sub lockf ($;$) { my
($param, $opts) = validate_pos(@_, 1, 0); $opts ||= {}; $opts =
validate(@{ [ $opts ] }, { blocking => 0, shared => 0, silent =>
0, # deprecated option, does nothing timeout => 0, mode => 0,
}); $opts = {%defaults, %$opts}; my ($fh, $fname); if (ref
$param eq "") { # filename instead of filehandle open $fh, ">>",
$param or die "Can't open $param: $!"; $fname = $param; } else {
$fh = $param; } unless (_loc