# Returns the process id currently stored in the file set. If the method # is passed a file handle, it will return the value, leaving the file handle # locked. This is useful for atomic operations where the caller needs to # write to the file after the read without allowing other dirty writes. # # Please note, when passing a file handle, caller is responsible for # closing it. Also, file handles must be passed by reference! sub read { my ($self, $fh) = @_; sysopen($fh, $self->{path}, O_RDWR|O_CREAT) || die qq/Cannot open pid file "$self->{path}": $!\n/; flock($fh, LOCK_EX | LOCK_NB) || die qq/pid "$self->{path}" already locked: $!\n/; # WARNING: Use of uninitialized value in pattern match (m//): my ($pid) = <$fh> =~ /^(\d+)/; close $fh if @_ == 1; $self->debug("read(\"$self->{path}\") = " . ($pid || "")); return $pid; }