DateTime::Format::Builder::Parser::Regex: 解析器构造函数
有人可以向我展示一个工作构造函数吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我和你同时开始阅读 doco。唷!
经过一番回溯,该模块似乎在幕后有效工作,并且 DateTime::Format::Builder 将其作为插件调用。因此,使用创建自己的解析类的常规习惯用法,我想它是:
还要看看 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:
Also have a look at the create_constructor() method in DateTime::Format::Builder, which sets up a default constructor.