package NILab::IntervalChecker; =head1 NAME NILab::IntervalChecker - interval checker =head1 SYNOPSIS use NILab::IntervalChecker; # for 6 hours interval $obj = NILab::IntervalChecker->new('lastupdate.log', 60 * 60 * 6); # for debug $obj->{debug} = 1; # interval check if($obj->check()){ print "interval ok\n"; }else{ print "interval no\n"; } =head1 AUTHOR NI-Lab. http://www.nilab.info/ =head1 LICENSE Copyright (c) 2005 NI-Lab. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 HISTORY Since: 2005-05-13 2005-06-06 * change debug output. STDOUT to STDERR. 2005-05-13 * add subroutine check =cut #============================================================================== use strict; use warnings; #============================================================================== # constructor #============================================================================== sub new { my $class = shift; my $self = { # initializeing field value filepath => shift, interval => shift, debug => 0, }; # my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; for hash params? bless $self,$class; return $self; } #============================================================================== # debug on/off # $obj->debug(1); #============================================================================== #sub debug { # my $self = shift; # $self->{debug} = shift; # return 1; #} #============================================================================== # check #============================================================================== sub check { my $self = shift; my $filepath = $self->{filepath}; my $interval = $self->{interval}; my $debug = $self->{debug}; my $FHIN; my $FHOUT; # get last updating time my $last_update_time; if(open($FHIN, "$filepath")){ $last_update_time = readline($FHIN); close($FHIN); print STDERR "OLD TIME: $last_update_time\n" if $debug; }else{ print STDERR "Could not open the file $filepath for reading.\n" if $debug; $last_update_time = 0; } # check interval time if(!((time - $last_update_time) > $interval)){ print STDERR "Interval NG.\n" if $debug; return 0; } print STDERR "Interval OK.\n" if $debug; # write if(!open($FHOUT, ">$filepath")){ print STDERR "Could not open the file $filepath for writing.\n" if $debug; return 0; } print $FHOUT time; close($FHOUT); return 1; } #============================================================================== 1;