如何使用 DateTime 对象执行函数?

发布于 2024-09-14 13:27:43 字数 184 浏览 0 评论 0原文

我希望我的 .pl 在每天、每周和每月结束时执行一个函数。该函数将在一天结束时从 html 中获取一个整数并将其存储在 .txt 中以用于创建图表。

我已经知道如何使用 perl 脚本实现 CGI 和 JS,所以这不是问题。我只是不清楚 DateTime 对象是否可以以这种布尔类型的方式使用。这听起来很简单,所以我希望有一个简单的答案。

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

I already know how to implement CGI and JS with a perl script, so that is no issue. I'm just not clear on whether or not DateTime objects can be used in this boolean-type way. This sounds simple, so I hope there is a simple answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2024-09-21 13:27:43

目前尚不清楚您的解决方案是否必须是 all-perl。

如果不是 all-perl,您可以使用系统的调度程序(cron on Unix/Linux/MacOS、AT 或 Windows 上的 Windows 调度程序)。

对于月底,您可以每天运行该脚本,但在月初,如果不是月底则退出。月末(以及周末作为奖励)的逻辑将使用类似以下内容:

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

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

If NOT all-perl, you can do something "at the end of every day" and "at teh end of every week" (the latter just means end of Sunday) using your system's scheduler (cron on Unix/Linux/MacOS, AT pr Windows Scheduler on Windows).

For end of month, you can run the script every day but at the beginning, quit if not end of month. The logic for both end of month (and end of week as a bonus) would be using something like:

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
}

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

命比纸薄 2024-09-21 13:27:43

下面是一个如何使用 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 &仅现在

CPAN 上有几个 DateTime 模块可能会提供更多帮助。

  • DateTime::Event::Cron
  • < a href="http://search.cpan.org/dist/DateTime-Cron-Simple/" rel="nofollow noreferrer">DateTime::Cron::Simple

或者是一个完整的 cron 克隆,名为 Schedule::Cron

Here is a simple example of how to use DateTime to trigger a function:

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
    }
}

The above will run late & now only.

There are a couple of DateTime modules on CPAN which may help further.

Alternatively there is a complete cron clone called Schedule::Cron

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文