Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/DateTime/Format/Builder/Parser/Regex.pm |
Statements | Executed 62 statements in 578µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 490µs | 636µs | BEGIN@73 | DateTime::Format::Builder::Parser::Regex::
7 | 1 | 1 | 78µs | 404µs | create_parser | DateTime::Format::Builder::Parser::Regex::
14 | 2 | 1 | 23µs | 23µs | __ANON__[:84] | DateTime::Format::Builder::Parser::Regex::
1 | 1 | 1 | 14µs | 16µs | BEGIN@68 | DateTime::Format::Builder::Parser::Regex::
1 | 1 | 1 | 7µs | 36µs | BEGIN@69 | DateTime::Format::Builder::Parser::Regex::
1 | 1 | 1 | 7µs | 48µs | BEGIN@70 | DateTime::Format::Builder::Parser::Regex::
4 | 2 | 1 | 6µs | 6µs | __ANON__[:98] | DateTime::Format::Builder::Parser::Regex::
0 | 0 | 0 | 0s | 0s | do_match | DateTime::Format::Builder::Parser::Regex::
0 | 0 | 0 | 0s | 0s | make | DateTime::Format::Builder::Parser::Regex::
0 | 0 | 0 | 0s | 0s | post_match | DateTime::Format::Builder::Parser::Regex::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package DateTime::Format::Builder::Parser::Regex; | ||||
2 | |||||
3 | =head1 NAME | ||||
4 | |||||
5 | DateTime::Format::Builder::Parser::Regex - Regex based date parsing | ||||
6 | |||||
7 | =head1 SYNOPSIS | ||||
8 | |||||
9 | my $parser = DateTime::Format::Builder->create_parser( | ||||
10 | regex => qr/^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/, | ||||
11 | params => [ qw( year month day hour minute second ) ], | ||||
12 | ); | ||||
13 | |||||
14 | =head1 SPECIFICATION | ||||
15 | |||||
16 | In addition to the | ||||
17 | L<common keys|DateTime::Format::Builder/"SINGLE SPECIFICATIONS">, | ||||
18 | C<Regex> supports: | ||||
19 | |||||
20 | =over 4 | ||||
21 | |||||
22 | =item * | ||||
23 | |||||
24 | B<regex> is a regular expression that should capture | ||||
25 | elements of the datetime string. | ||||
26 | This is a required element. This is the key whose presence | ||||
27 | indicates it's a specification that belongs to this class. | ||||
28 | |||||
29 | =item * | ||||
30 | |||||
31 | B<params> is an arrayref of key names. The captures from the | ||||
32 | regex are mapped to these (C<$1> to the first element, C<$2> | ||||
33 | to the second, and so on) and handed to | ||||
34 | C<< DateTime->new() >>. | ||||
35 | This is a required element. | ||||
36 | |||||
37 | =item * | ||||
38 | |||||
39 | B<extra> is a hashref of extra arguments you wish to give to | ||||
40 | C<< DateTime->new() >>. For example, you could set the | ||||
41 | C<year> or C<time_zone> to defaults: | ||||
42 | |||||
43 | extra => { year => 2004, time_zone => "Australia/Sydney" }, | ||||
44 | |||||
45 | =item * | ||||
46 | |||||
47 | B<constructor> is either an arrayref or a coderef. If an arrayref | ||||
48 | then the first element is a class name or object, and the second | ||||
49 | element is a method name (or coderef since Perl allows that sort of | ||||
50 | thing). The arguments to the call are anything in C<$p> and | ||||
51 | anything given in the C<extra> option above. | ||||
52 | |||||
53 | If only a coderef is supplied, then it is called with arguments of | ||||
54 | C<$self>, C<$p> and C<extra>. | ||||
55 | |||||
56 | In short: | ||||
57 | |||||
58 | $self->$coderef( %$p, %{ $self->{extra} } ); | ||||
59 | |||||
60 | The method is expected to return a valid L<DateTime> object, | ||||
61 | or undef in event of failure, but can conceivably return anything | ||||
62 | it likes. So long as it's 'true'. | ||||
63 | |||||
64 | =back | ||||
65 | |||||
66 | =cut | ||||
67 | |||||
68 | 3 | 28µs | 2 | 18µs | # spent 16µs (14+2) within DateTime::Format::Builder::Parser::Regex::BEGIN@68 which was called:
# once (14µs+2µs) by DateTime::Format::Builder::Parser::BEGIN@1.40 at line 68 # spent 16µs making 1 call to DateTime::Format::Builder::Parser::Regex::BEGIN@68
# spent 2µs making 1 call to strict::import |
69 | 3 | 21µs | 2 | 65µs | # spent 36µs (7+29) within DateTime::Format::Builder::Parser::Regex::BEGIN@69 which was called:
# once (7µs+29µs) by DateTime::Format::Builder::Parser::BEGIN@1.40 at line 69 # spent 36µs making 1 call to DateTime::Format::Builder::Parser::Regex::BEGIN@69
# spent 29µs making 1 call to vars::import |
70 | 3 | 27µs | 2 | 89µs | # spent 48µs (7+41) within DateTime::Format::Builder::Parser::Regex::BEGIN@70 which was called:
# once (7µs+41µs) by DateTime::Format::Builder::Parser::BEGIN@1.40 at line 70 # spent 48µs making 1 call to DateTime::Format::Builder::Parser::Regex::BEGIN@70
# spent 41µs making 1 call to Exporter::import |
71 | |||||
72 | 1 | 500ns | $VERSION = '0.77'; | ||
73 | 3 | 352µs | 1 | 636µs | # spent 636µs (490+146) within DateTime::Format::Builder::Parser::Regex::BEGIN@73 which was called:
# once (490µs+146µs) by DateTime::Format::Builder::Parser::BEGIN@1.40 at line 73 # spent 636µs making 1 call to DateTime::Format::Builder::Parser::Regex::BEGIN@73 |
74 | 1 | 9µs | @ISA = qw( DateTime::Format::Builder::Parser::generic ); | ||
75 | |||||
76 | __PACKAGE__->valid_params( | ||||
77 | # How to match | ||||
78 | params => { | ||||
79 | type => ARRAYREF, # mapping $1,$2,... to new() args | ||||
80 | }, | ||||
81 | regex => { | ||||
82 | type => SCALARREF, | ||||
83 | callbacks => { | ||||
84 | 14 | 43µs | # spent 23µs within DateTime::Format::Builder::Parser::Regex::__ANON__[/2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/DateTime/Format/Builder/Parser/Regex.pm:84] which was called 14 times, avg 2µs/call:
# 7 times (12µs+0s) by Params::Validate::XS::validate at line 311 of DateTime/Format/Builder/Parser.pm, avg 2µs/call
# 7 times (11µs+0s) by Params::Validate::XS::validate at line 333 of DateTime/Format/Builder/Parser.pm, avg 2µs/call | ||
85 | } | ||||
86 | }, | ||||
87 | # How to create | ||||
88 | extra => { | ||||
89 | type => HASHREF, | ||||
90 | optional => 1, | ||||
91 | }, | ||||
92 | constructor => { | ||||
93 | type => CODEREF|ARRAYREF, | ||||
94 | optional => 1, | ||||
95 | callbacks => { | ||||
96 | # spent 6µs within DateTime::Format::Builder::Parser::Regex::__ANON__[/2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/DateTime/Format/Builder/Parser/Regex.pm:98] which was called 4 times, avg 2µs/call:
# 2 times (4µs+0s) by Params::Validate::XS::validate at line 311 of DateTime/Format/Builder/Parser.pm, avg 2µs/call
# 2 times (3µs+0s) by Params::Validate::XS::validate at line 333 of DateTime/Format/Builder/Parser.pm, avg 1µs/call | ||||
97 | 4 | 14µs | ref($_[0]) eq 'ARRAY' ? (@{$_[0]} == 2) : 1 | ||
98 | } | ||||
99 | } | ||||
100 | }, | ||||
101 | 1 | 12µs | 1 | 13µs | ); # spent 13µs making 1 call to DateTime::Format::Builder::Parser::valid_params |
102 | |||||
103 | sub do_match | ||||
104 | { | ||||
105 | my $self = shift; | ||||
106 | my $date = shift; | ||||
107 | my @matches = $date =~ $self->{regex}; | ||||
108 | return @matches ? \@matches : undef; | ||||
109 | } | ||||
110 | |||||
111 | sub post_match | ||||
112 | { | ||||
113 | my $self = shift; | ||||
114 | my ( $date, $matches, $p ) = @_; | ||||
115 | # Fill %p from match | ||||
116 | @{$p}{ @{ $self->{params} } } = @$matches; | ||||
117 | return; | ||||
118 | } | ||||
119 | |||||
120 | sub make { | ||||
121 | my $self = shift; | ||||
122 | my ( $date, $dt, $p ) = @_; | ||||
123 | my @args = ( %$p, %{ $self->{extra} } ); | ||||
124 | if (my $cons = $self->{constructor}) | ||||
125 | { | ||||
126 | if (ref $cons eq 'ARRAY') { | ||||
127 | my ($class, $method) = @$cons; | ||||
128 | return $class->$method(@args); | ||||
129 | } elsif (ref $cons eq 'CODE') { | ||||
130 | return $self->$cons( @args ); | ||||
131 | } | ||||
132 | } | ||||
133 | else | ||||
134 | { | ||||
135 | return DateTime->new(@args); | ||||
136 | } | ||||
137 | } | ||||
138 | |||||
139 | sub create_parser | ||||
140 | # spent 404µs (78+326) within DateTime::Format::Builder::Parser::Regex::create_parser which was called 7 times, avg 58µs/call:
# 7 times (78µs+326µs) by DateTime::Format::Builder::Parser::create_single_parser at line 334 of DateTime/Format/Builder/Parser.pm, avg 58µs/call | ||||
141 | 7 | 8µs | my ($self, %args) = @_; | ||
142 | 7 | 3µs | $args{extra} ||= {}; | ||
143 | 7 | 18µs | 7 | 27µs | unless (ref $self) # spent 27µs making 7 calls to DateTime::Format::Builder::Parser::generic::new, avg 4µs/call |
144 | { | ||||
145 | $self = $self->new( %args ); | ||||
146 | } | ||||
147 | |||||
148 | # Create our parser | ||||
149 | return $self->generic_parser( | ||||
150 | 7 | 37µs | 7 | 299µs | ( map { exists $args{$_} ? ( $_ => $args{$_} ) : () } qw( # spent 299µs making 7 calls to DateTime::Format::Builder::Parser::generic::generic_parser, avg 43µs/call |
151 | on_match on_fail preprocess postprocess | ||||
152 | ) ), | ||||
153 | label => $args{label}, | ||||
154 | ); | ||||
155 | } | ||||
156 | |||||
157 | |||||
158 | 1 | 6µs | 1; | ||
159 | |||||
160 | __END__ |