DateTime::Format::Builder::Parser::Regex: 解析器构造函数

发布于 2024-10-09 02:36:48 字数 870 浏览 7 评论 0原文

有人可以向我展示一个工作构造函数吗?
DateTime::Format::Builder::解析器::正则表达式

#!/usr/bin/env perl
use warnings;
use 5.012;
use DateTime::Format::Builder;
use DateTime::Format::Builder::Parser::Regex;

my $parser = DateTime::Format::Builder->create_parser(
    regex  => qr/^(\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/,
    length => 13,
    params => [ qw( year month day hour minute second ) ],
    postprocess => \&_fix_year,
    extra => {time_zone => "Australia/Sydney" },
    constructor => ..., ###
);

sub _fix_year {
    my %args = @_;
    my ( $date, $p ) = @args{ qw( input parsed ) };
    $p->{year} += $p->{year} > 69 ? 1900 : 2000;
    return 1;
}

编辑: 更改了正则表达式,因此后处理 fix_year 确实更有意义;

Could someone show me a working constructor?
DateTime::Format::Builder::Parser::Regex

#!/usr/bin/env perl
use warnings;
use 5.012;
use DateTime::Format::Builder;
use DateTime::Format::Builder::Parser::Regex;

my $parser = DateTime::Format::Builder->create_parser(
    regex  => qr/^(\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/,
    length => 13,
    params => [ qw( year month day hour minute second ) ],
    postprocess => \&_fix_year,
    extra => {time_zone => "Australia/Sydney" },
    constructor => ..., ###
);

sub _fix_year {
    my %args = @_;
    my ( $date, $p ) = @args{ qw( input parsed ) };
    $p->{year} += $p->{year} > 69 ? 1900 : 2000;
    return 1;
}

edit:
Changed regex so postprocess fix_year does make more sense;

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

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

发布评论

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

评论(1

清风夜微凉 2024-10-16 02:36:48

我和你同时开始阅读 doco。唷!

经过一番回溯,该模块似乎在幕后有效工作,并且 DateTime::Format::Builder 将其作为插件调用。因此,使用创建自己的解析类的常规习惯用法,我想它是:

#!/usr/bin/perl
package MyDateParser;
use common::sense;

use DateTime;
use DateTime::Format::Builder;
use DateTime::Format::Builder::Parser::Regex;

use DateTime::Format::Builder(
    parsers => {
        parse_datetime => {
            regex  => qr/^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/,
            length => 15,
            params => [ qw( year month day hour minute second ) ],
            postprocess => \&_fix_year,
            extra => {time_zone => "Australia/Sydney" },
            constructor => \&_construct_date,
        }
    }
);

sub _fix_year {
    my %args = @_;
    my ( $date, $p ) = @args{ qw( input parsed ) };
    $p->{year} += $p->{year} > 69 ? 1900 : 2000;
    return 1;
}

sub _construct_date {
    my ($p, %extra) = @_;
    use Data::Dumper; warn Dumper {p => $p, extra => \%extra};
    return DateTime->new( %extra );
}

#-----------------------------------------------------------------------

package main;

my $dt = MyDateParser->parse_datetime('00101223T094517');

还要看看 DateTime::Format::Builder 中的 create_constructor() 方法,它设置了默认构造函数。

I started reading the doco at the same point as you. Phew!

After a bit of back-tracking, it appears that this module effectively works behind the scenes and DateTime::Format::Builder calls it as a plugin. So using the regular idiom of creating your own parse class, I'm thinking its:

#!/usr/bin/perl
package MyDateParser;
use common::sense;

use DateTime;
use DateTime::Format::Builder;
use DateTime::Format::Builder::Parser::Regex;

use DateTime::Format::Builder(
    parsers => {
        parse_datetime => {
            regex  => qr/^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/,
            length => 15,
            params => [ qw( year month day hour minute second ) ],
            postprocess => \&_fix_year,
            extra => {time_zone => "Australia/Sydney" },
            constructor => \&_construct_date,
        }
    }
);

sub _fix_year {
    my %args = @_;
    my ( $date, $p ) = @args{ qw( input parsed ) };
    $p->{year} += $p->{year} > 69 ? 1900 : 2000;
    return 1;
}

sub _construct_date {
    my ($p, %extra) = @_;
    use Data::Dumper; warn Dumper {p => $p, extra => \%extra};
    return DateTime->new( %extra );
}

#-----------------------------------------------------------------------

package main;

my $dt = MyDateParser->parse_datetime('00101223T094517');

Also have a look at the create_constructor() method in DateTime::Format::Builder, which sets up a default constructor.

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