How to execute a function using DateTime objects?

I want my .pl to function at the end of every day, week and month. What the function will do is grab an integer from html at the end of the day and save it in .txt, which will be used to create the graph.

I already know how to implement CGI and JS with a perl script, so this is not a problem. I just don't understand if DateTime objects can be used in this boolean type. It sounds simple, so I hope there is a simple answer.

+3
source share
2 answers

It is unclear whether your solution MUST be all-perl or not.

all-perl, - " " " " ( ), (cron Unix/Linux/MacOS, AT pr Windows Windows).

script , , , . ( ) - :

use Time::Local qw ( timelocal_nocheck ); 
my @time_data = localtime() ; 
my $current_dom = $time_data[3];
my $current_dow = $time_data[6];
$time_data[4]++;   # Next month.
$time_data[3] = 0; # Last day of previous month.
@time_data = localtime ( timelocal_nocheck ( @time_data ) );
if ($current_dom == $time_data[3]) {
    # Do end of month processing
}
if ($current_dow == 0) { # Sunday
    # Do end of week processing
}

: http://www.perlmonks.org/?node_id=418897

+3

, DateTime :

use 5.012;
use warnings;
use DateTime;

my $now = DateTime->now;

# example queue
my @jobs = (
    { after => $now->clone->subtract( hours => 1 ), sub => undef },  # done
    { after => $now->clone->subtract( hours => 1 ), sub => sub{ say "late"  }},
    { after => $now->clone,                         sub => sub{ say "now"  }},
    { after => $now->clone->add( hours => 1 ),      sub => sub{ say "not yet" }},
);

for my $job (@jobs) {
    if ($job->{after} <= $now && $job->{sub}) {
        $job->{sub}->();
        $job->{sub} = undef;   # ie. clear from queue
    }
}

late now.

CPAN DateTime, .

-, Schedule::Cron

0

Source: https://habr.com/ru/post/1759940/


All Articles