File-Pid

 view release on metacpan or  search on metacpan

lib/File/Pid.pm  view on Meta::CPAN

package File::Pid;
# $Id: Pid.pm,v 1.1 2005/01/11 13:09:54 cwest Exp $
use strict;

=head1 NAME

File::Pid - Pid File Manipulation

=head1 SYNOPSIS

  use File::Pid;
  
  my $pidfile = File::Pid->new({
    file => '/some/file.pid',
  });
  
  $pidfile->write;
  
  if ( my $num = $pidfile->running ) {
      die "Already running: $num\n";
  }

  $pidfile->remove;

=cut

use vars qw[$VERSION];
$VERSION = sprintf "%d.%02d", split m/\./, (qw$Revision: 1.1 $)[1];

use File::Spec::Functions qw[tmpdir catfile];
use File::Basename qw[basename];
use base qw[Class::Accessor::Fast];

=head1 DESCRIPTION

This software manages a pid file for you. It will create a pid file,
query the process within to discover if it's still running, and remove
the pid file.

=head2 new

  my $pidfile = File::Pid->new;

  my $thisfile = File::Pid->new({
    file => '/var/run/daemon.pid',
  });

  my $thisfileandpid = File::Pid->new({
    file => '/var/run/daemon.pid',
    pid  => '145',
  });

This constructor takes two optional paramters.

C<file> - The name of the pid file to work on. If not specified, a pid
file located in C<< File::Spec->tmpdir() >> will be created that matches
C<< (File::Basename::basename($0))[0] . '.pid' >>. So, for example, if
C<$0> is F<~/bin/sig.pl>, the pid file will be F</tmp/sig.pl.pid>.

C<pid> - The pid to write to a new pidfile. If not specified, C<$$> is
used when the pid file doesn't exist. When the pid file does exist, the
pid inside it is used.

=head2 file

  my $pidfile = $pidfile->file;

Accessor/mutator for the filename used as the pid file.

=head2 pid

  my $pid = $pidfile->pid;

Accessor/mutator for the pid being saved to the pid file.

=cut

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new(@_);
    $self->_get_pidfile;
    $self->_get_pid;
    return $self;
}
__PACKAGE__->mk_accessors(qw[file pid]);

=head2 write



( run in 0.506 second using v1.01-cache-2.11-cpan-71847e10f99 )