package DailyOnce; use File::Basename; our $CVSVERSION = '$Revision: 1.4 $'; ##################################### sub new { ##################################### my($class, @options) = @_; my $self = { tmpfile => "/tmp/" . basename($0) . ".dat", @options, }; bless $self, $class; return $self; } ##################################### sub done_for_today { ##################################### my($self) = @_; unless(-e $self->{tmpfile}) { $self->update(); return 0; } open FILE, "<$self->{tmpfile}" or die "Cannot open $self->{tmpfile} ($!)"; my $data = ; chomp $data; close FILE; my $today = $self->todays_date(); return 1 if $data eq $today; $self->update(); return 0; } ##################################### sub update { ##################################### my($self) = @_; open FILE, ">$self->{tmpfile}" or die "Cannot open $self->{tmpfile} ($!)"; my $today = $self->todays_date(); print FILE "$today\n"; close FILE; } ##################################### sub todays_date { ##################################### my($self, $time) = @_; $time = time() unless defined $time; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); return sprintf "%d/%02d/%02d", $year + 1900, $mon + 1, $mday; } 1; __END__ =head1 NAME DailyOnce.pm - Utility to run scripts exactly once a day =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS if(DailyOnce->new()->done_for_today()) { exit 0; } =head1 DESCRIPTION B helps running scripts only once a day, even if they're being called several times a day, e.g. via a cronjob. =head2 METHODS =over 4 =item C<> Create a new C object. Optional parameter: C, to specify the file where C stores the date: my $do = DailyOnce->new(tmpfile => "/tmp/foobar"); The name of the file defaults to C. =item C<> Checks if the script has been called already today and returns 1 if so and 0 otherwise. It will create a file to memorize this check. If C will be called the next time within the same day, it will return 1. When it will be called the next time on a new day, it will return 0. =back =head1 EXAMPLES use Getopt::Std; use DailyOnce; getopts('o', \my %opts); if($opts{o} and DailyOnce->new()->done_for_today()) { print "Done for today already\n"; exit 0; } # Normal processing ... =head1 LEGALESE Copyright 2004 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2004, Mike Schilli