line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DateTime::Format::Builder; |
2
|
|
|
|
|
|
|
|
3
|
23
|
|
|
23
|
|
2016436
|
use strict; |
|
23
|
|
|
|
|
225
|
|
|
23
|
|
|
|
|
689
|
|
4
|
23
|
|
|
23
|
|
112
|
use warnings; |
|
23
|
|
|
|
|
37
|
|
|
23
|
|
|
|
|
820
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.83'; |
7
|
|
|
|
|
|
|
|
8
|
23
|
|
|
23
|
|
113
|
use Carp; |
|
23
|
|
|
|
|
39
|
|
|
23
|
|
|
|
|
1259
|
|
9
|
23
|
|
|
23
|
|
18441
|
use DateTime 1.00; |
|
23
|
|
|
|
|
10514834
|
|
|
23
|
|
|
|
|
2403
|
|
10
|
23
|
|
|
|
|
5446
|
use Params::Validate 0.72 qw( |
11
|
|
|
|
|
|
|
validate SCALAR ARRAYREF HASHREF SCALARREF CODEREF GLOB GLOBREF UNDEF |
12
|
23
|
|
|
23
|
|
13183
|
); |
|
23
|
|
|
|
|
60446
|
|
13
|
|
|
|
|
|
|
our %dispatch_data; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $parser = 'DateTime::Format::Builder::Parser'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub verbose { |
18
|
0
|
|
|
0
|
1
|
0
|
warn "Use of verbose() deprecated for the interim."; |
19
|
0
|
|
|
|
|
0
|
1; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub import { |
23
|
36
|
|
|
36
|
|
21118
|
my $class = shift; |
24
|
36
|
100
|
|
|
|
44964
|
$class->create_class( @_, class => (caller)[0] ) if @_; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub create_class { |
28
|
21
|
|
|
21
|
1
|
8091
|
my $class = shift; |
29
|
21
|
|
|
|
|
856
|
my %args = validate( |
30
|
|
|
|
|
|
|
@_, |
31
|
|
|
|
|
|
|
{ |
32
|
|
|
|
|
|
|
class => { type => SCALAR, default => (caller)[0] }, |
33
|
|
|
|
|
|
|
version => { type => SCALAR, optional => 1 }, |
34
|
|
|
|
|
|
|
verbose => { type => SCALAR | GLOBREF | GLOB, optional => 1 }, |
35
|
|
|
|
|
|
|
parsers => { type => HASHREF }, |
36
|
|
|
|
|
|
|
groups => { type => HASHREF, optional => 1 }, |
37
|
|
|
|
|
|
|
constructor => |
38
|
|
|
|
|
|
|
{ type => UNDEF | SCALAR | CODEREF, optional => 1 }, |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
21
|
50
|
|
|
|
172
|
verbose( $args{verbose} ) if exists $args{verbose}; |
43
|
|
|
|
|
|
|
|
44
|
21
|
|
|
|
|
53
|
my $target = $args{class}; # where we're writing our methods and such. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Create own lovely new package |
47
|
|
|
|
|
|
|
{ |
48
|
23
|
|
|
23
|
|
182
|
no strict 'refs'; |
|
23
|
|
|
|
|
42
|
|
|
23
|
|
|
|
|
7344
|
|
|
21
|
|
|
|
|
237
|
|
49
|
|
|
|
|
|
|
|
50
|
21
|
100
|
|
|
|
64
|
${"${target}::VERSION"} = $args{version} if exists $args{version}; |
|
4
|
|
|
|
|
34
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$class->create_constructor( |
53
|
|
|
|
|
|
|
$target, exists $args{constructor}, |
54
|
|
|
|
|
|
|
$args{constructor} |
55
|
21
|
|
|
|
|
112
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Turn groups of parser specs in to groups of parsers |
58
|
|
|
|
|
|
|
{ |
59
|
19
|
|
|
|
|
39
|
my $specs = $args{groups}; |
|
19
|
|
|
|
|
39
|
|
60
|
19
|
|
|
|
|
29
|
my %groups; |
61
|
|
|
|
|
|
|
|
62
|
19
|
|
|
|
|
908
|
for my $label ( keys %$specs ) { |
63
|
3
|
|
|
|
|
5
|
my $parsers = $specs->{$label}; |
64
|
3
|
|
|
|
|
11
|
my $code = $class->create_parser($parsers); |
65
|
3
|
|
|
|
|
6
|
$groups{$label} = $code; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
19
|
|
|
|
|
74
|
$dispatch_data{$target} = \%groups; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Write all our parser methods, creating parsers as we go. |
72
|
19
|
|
|
|
|
71
|
while ( my ( $method, $parsers ) = each %{ $args{parsers} } ) { |
|
36
|
|
|
|
|
408
|
|
73
|
19
|
|
|
|
|
57
|
my $globname = $target . "::$method"; |
74
|
|
|
|
|
|
|
croak "Will not override a preexisting method $method()" |
75
|
19
|
100
|
|
|
|
34
|
if defined &{$globname}; |
|
19
|
|
|
|
|
1176
|
|
76
|
17
|
|
|
|
|
68
|
*$globname = $class->create_end_parser($parsers); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub create_constructor { |
83
|
21
|
|
|
21
|
0
|
41
|
shift; |
84
|
21
|
|
|
|
|
103
|
my ( $target, $intended, $value ) = @_; |
85
|
|
|
|
|
|
|
|
86
|
21
|
|
|
|
|
119
|
my $new = $target . "::new"; |
87
|
21
|
100
|
|
|
|
64
|
$value = 1 unless $intended; |
88
|
|
|
|
|
|
|
|
89
|
21
|
100
|
|
|
|
58
|
return unless $value; |
90
|
18
|
100
|
100
|
|
|
140
|
return if not $intended and defined &$new; |
91
|
16
|
100
|
|
|
|
1779
|
croak "Will not override a preexisting constructor new()" |
92
|
|
|
|
|
|
|
if defined &$new; |
93
|
|
|
|
|
|
|
|
94
|
23
|
|
|
23
|
|
166
|
no strict 'refs'; |
|
23
|
|
|
|
|
58
|
|
|
23
|
|
|
|
|
17664
|
|
95
|
|
|
|
|
|
|
|
96
|
14
|
100
|
|
|
|
53
|
return *$new = $value if ref $value eq 'CODE'; |
97
|
|
|
|
|
|
|
return *$new = sub { |
98
|
16
|
|
|
16
|
|
13013
|
my $class = shift; |
99
|
16
|
100
|
|
|
|
225
|
croak "${class}->new takes no parameters." if @_; |
100
|
|
|
|
|
|
|
|
101
|
15
|
|
66
|
|
|
76
|
my $self = bless {}, ref($class) || $class; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# If called on an object, clone, but we've nothing to |
104
|
|
|
|
|
|
|
# clone |
105
|
|
|
|
|
|
|
|
106
|
15
|
|
|
|
|
38
|
$self; |
107
|
12
|
|
|
|
|
92
|
}; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub create_parser { |
111
|
40
|
|
|
40
|
0
|
24196
|
my $class = shift; |
112
|
40
|
|
|
|
|
104
|
my @common = ( maker => $class ); |
113
|
40
|
100
|
|
|
|
127
|
if ( @_ == 1 ) { |
114
|
27
|
|
|
|
|
79
|
my $parsers = shift; |
115
|
27
|
50
|
|
|
|
137
|
my @parsers = ( |
|
|
100
|
|
|
|
|
|
116
|
|
|
|
|
|
|
( ref $parsers eq 'HASH' ) |
117
|
|
|
|
|
|
|
? %$parsers |
118
|
|
|
|
|
|
|
: ( ( ref $parsers eq 'ARRAY' ) ? @$parsers : $parsers ) |
119
|
|
|
|
|
|
|
); |
120
|
27
|
|
|
|
|
212
|
$parser->create_parser( \@common, @parsers ); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
else { |
123
|
13
|
|
|
|
|
54
|
$parser->create_parser( \@common, @_ ); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# This creates the end methods. Coderefs die on bad parses, return C<DateTime> |
128
|
|
|
|
|
|
|
# objects on good parse. |
129
|
|
|
|
|
|
|
sub create_end_parser { |
130
|
23
|
|
|
23
|
0
|
65
|
my ( $class, $parsers ) = @_; |
131
|
23
|
|
|
|
|
81
|
$class->create_method( $class->create_parser($parsers) ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub create_method { |
135
|
23
|
|
|
23
|
1
|
88
|
shift; |
136
|
23
|
|
|
|
|
49
|
my ($parser) = @_; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
return sub { |
139
|
31
|
|
|
31
|
|
12851
|
my $self = shift; |
140
|
31
|
|
|
|
|
128
|
$parser->parse( $self, @_ ); |
141
|
23
|
|
|
|
|
159
|
}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub on_fail { |
145
|
3
|
|
|
3
|
1
|
7
|
shift; |
146
|
3
|
|
|
|
|
6
|
my ($input) = @_; |
147
|
|
|
|
|
|
|
|
148
|
3
|
|
|
|
|
5
|
my $pkg; |
149
|
3
|
|
|
|
|
5
|
my $i = 0; |
150
|
3
|
|
|
|
|
25
|
while ( ($pkg) = caller( $i++ ) ) { |
151
|
|
|
|
|
|
|
last |
152
|
15
|
100
|
100
|
|
|
111
|
if ( !UNIVERSAL::isa( $pkg, 'DateTime::Format::Builder' ) |
153
|
|
|
|
|
|
|
&& !UNIVERSAL::isa( $pkg, 'DateTime::Format::Builder::Parser' ) ); |
154
|
|
|
|
|
|
|
} |
155
|
3
|
|
|
|
|
7
|
local $Carp::CarpLevel = $i; |
156
|
3
|
|
|
|
|
675
|
croak "Invalid date format: $input"; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub new { |
160
|
11
|
|
|
11
|
1
|
1396
|
my $class = shift; |
161
|
11
|
100
|
|
|
|
205
|
croak "Constructor 'new' takes no parameters" if @_; |
162
|
|
|
|
|
|
|
my $self = bless { |
163
|
1
|
|
|
1
|
|
102
|
parser => sub { croak "No parser set." } |
164
|
|
|
|
|
|
|
}, |
165
|
10
|
|
66
|
|
|
77
|
ref($class) || $class; |
166
|
10
|
100
|
|
|
|
29
|
if ( ref $class ) { |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# If called on an object, clone |
169
|
2
|
|
|
|
|
7
|
$self->set_parser( $class->get_parser ); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# and that's it. we don't store that much info per object |
172
|
|
|
|
|
|
|
} |
173
|
10
|
|
|
|
|
59
|
return $self; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub parser { |
177
|
6
|
|
|
6
|
1
|
2238
|
my $class = shift; |
178
|
6
|
|
|
|
|
58
|
my $parser = $class->create_end_parser( \@_ ); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Do we need to instantiate a new object for return, |
181
|
|
|
|
|
|
|
# or are we modifying an existing object? |
182
|
6
|
|
|
|
|
10
|
my $self; |
183
|
6
|
50
|
|
|
|
27
|
$self = ref $class ? $class : $class->new; |
184
|
|
|
|
|
|
|
|
185
|
6
|
|
|
|
|
22
|
$self->set_parser($parser); |
186
|
|
|
|
|
|
|
|
187
|
6
|
|
|
|
|
15
|
$self; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub clone { |
191
|
1
|
|
|
1
|
1
|
513
|
my $self = shift; |
192
|
1
|
50
|
|
|
|
6
|
croak "Calling object method as class method!" unless ref $self; |
193
|
1
|
|
|
|
|
3
|
return $self->new; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub set_parser { |
197
|
8
|
|
|
8
|
1
|
19
|
my ( $self, $parser ) = @_; |
198
|
8
|
50
|
33
|
|
|
49
|
croak "set_parser given something other than a coderef" |
199
|
|
|
|
|
|
|
unless $parser |
200
|
|
|
|
|
|
|
and ref $parser eq 'CODE'; |
201
|
8
|
|
|
|
|
42
|
$self->{parser} = $parser; |
202
|
8
|
|
|
|
|
12
|
$self; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub get_parser { |
206
|
6
|
|
|
6
|
1
|
563
|
my ($self) = @_; |
207
|
6
|
|
|
|
|
14
|
return $self->{parser}; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub parse_datetime { |
211
|
14
|
|
|
14
|
1
|
20642
|
my $self = shift; |
212
|
14
|
50
|
33
|
|
|
116
|
croak "parse_datetime is an object method, not a class method." |
213
|
|
|
|
|
|
|
unless ref $self and $self->isa(__PACKAGE__); |
214
|
14
|
50
|
|
|
|
36
|
croak "No date specified." unless @_; |
215
|
14
|
|
|
|
|
40
|
return $self->{parser}->( $self, @_ ); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub format_datetime { |
219
|
0
|
|
|
0
|
1
|
|
croak __PACKAGE__ . "::format_datetime not implemented."; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
require DateTime::Format::Builder::Parser; |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
1; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# ABSTRACT: Create DateTime parser classes and objects. |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
__END__ |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=pod |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=encoding UTF-8 |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head1 NAME |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
DateTime::Format::Builder - Create DateTime parser classes and objects. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 VERSION |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
version 0.83 |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head1 SYNOPSIS |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
package DateTime::Format::Brief; |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
use DateTime::Format::Builder ( |
247
|
|
|
|
|
|
|
parsers => { |
248
|
|
|
|
|
|
|
parse_datetime => [ |
249
|
|
|
|
|
|
|
{ |
250
|
|
|
|
|
|
|
regex => qr/^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/, |
251
|
|
|
|
|
|
|
params => [qw( year month day hour minute second )], |
252
|
|
|
|
|
|
|
}, |
253
|
|
|
|
|
|
|
{ |
254
|
|
|
|
|
|
|
regex => qr/^(\d{4})(\d\d)(\d\d)$/, |
255
|
|
|
|
|
|
|
params => [qw( year month day )], |
256
|
|
|
|
|
|
|
}, |
257
|
|
|
|
|
|
|
], |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
); |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 DESCRIPTION |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
DateTime::Format::Builder creates DateTime parsers. Many string formats of |
264
|
|
|
|
|
|
|
dates and times are simple and just require a basic regular expression to |
265
|
|
|
|
|
|
|
extract the relevant information. Builder provides a simple way to do this |
266
|
|
|
|
|
|
|
without writing reams of structural code. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Builder provides a number of methods, most of which you'll never need, or at |
269
|
|
|
|
|
|
|
least rarely need. They're provided more for exposing of the module's innards |
270
|
|
|
|
|
|
|
to any subclasses, or for when you need to do something slightly beyond what I |
271
|
|
|
|
|
|
|
expected. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 TUTORIAL |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
See L<DateTime::Format::Builder::Tutorial>. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head1 ERROR HANDLING AND BAD PARSES |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Often, I will speak of C<undef> being returned, however that's not strictly |
280
|
|
|
|
|
|
|
true. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
When a simple single specification is given for a method, the method isn't |
283
|
|
|
|
|
|
|
given a single parser directly. It's given a wrapper that will call C<on_fail> |
284
|
|
|
|
|
|
|
if the single parser returns C<undef>. The single parser must return C<undef> |
285
|
|
|
|
|
|
|
so that a multiple parser can work nicely and actual errors can be thrown from |
286
|
|
|
|
|
|
|
any of the callbacks. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
Similarly, any multiple parsers will only call C<on_fail> right at the end |
289
|
|
|
|
|
|
|
when it's tried all it could. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
C<on_fail> (see L<later|/on_fail>) is defined, by default, to throw an error. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Multiple parser specifications can also specify C<on_fail> with a coderef as |
294
|
|
|
|
|
|
|
an argument in the options block. This will take precedence over the |
295
|
|
|
|
|
|
|
inheritable and overrideable method. |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
That said, don't throw real errors from callbacks in multiple parser |
298
|
|
|
|
|
|
|
specifications unless you really want parsing to stop right there and not try |
299
|
|
|
|
|
|
|
any other parsers. |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
In summary: calling a B<method> will result in either a C<DateTime> object |
302
|
|
|
|
|
|
|
being returned or an error being thrown (unless you've overridden C<on_fail> |
303
|
|
|
|
|
|
|
or C<create_method>, or you've specified a C<on_fail> key to a multiple |
304
|
|
|
|
|
|
|
parser specification). |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
Individual B<parsers> (be they multiple parsers or single parsers) will return |
307
|
|
|
|
|
|
|
either the C<DateTime> object or C<undef>. |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 SINGLE SPECIFICATIONS |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
A single specification is a hash ref of instructions on how to create a |
312
|
|
|
|
|
|
|
parser. |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
The precise set of keys and values varies according to parser type. There are |
315
|
|
|
|
|
|
|
some common ones though: |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=over 4 |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=item * length |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
B<length> is an optional parameter that can be used to specify that this |
322
|
|
|
|
|
|
|
particular I<regex> is only applicable to strings of a certain fixed |
323
|
|
|
|
|
|
|
length. This can be used to make parsers more efficient. It's strongly |
324
|
|
|
|
|
|
|
recommended that any parser that can use this parameter does. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
You may happily specify the same length twice. The parsers will be tried in |
327
|
|
|
|
|
|
|
order of specification. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
You can also specify multiple lengths by giving it an arrayref of numbers |
330
|
|
|
|
|
|
|
rather than just a single scalar. If doing so, please keep the number of |
331
|
|
|
|
|
|
|
lengths to a minimum. |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
If any specifications without I<length>s are given and the particular |
334
|
|
|
|
|
|
|
I<length> parser fails, then the non-I<length> parsers are tried. |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
This parameter is ignored unless the specification is part of a multiple |
337
|
|
|
|
|
|
|
parser specification. |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=item * label |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
B<label> provides a name for the specification and is passed to some of the |
342
|
|
|
|
|
|
|
callbacks about to mentioned. |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=item * on_match and on_fail |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
B<on_match> and B<on_fail> are callbacks. Both routines will be called with |
347
|
|
|
|
|
|
|
parameters of: |
348
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
=over 4 |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
=item * input |
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
B<input> is the input to the parser (after any preprocessing callbacks). |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
=item * label |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
B<label> is the label of the parser if there is one. |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=item * self |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
B<self> is the object on which the method has been invoked (which may just be |
362
|
|
|
|
|
|
|
a class name). Naturally, you can then invoke your own methods on it do get |
363
|
|
|
|
|
|
|
information you want. |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
=item * |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
B<args> is an arrayref of any passed arguments, if any. If there were no |
368
|
|
|
|
|
|
|
arguments, then this parameter is not given. |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=back |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
These routines will be called depending on whether the B<regex> match |
373
|
|
|
|
|
|
|
succeeded or failed. |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
=item * preprocess |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
B<preprocess> is a callback provided for cleaning up input prior to |
378
|
|
|
|
|
|
|
parsing. It's given a hash as arguments with the following keys: |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
=over 4 |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=item * input |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
B<input> is the datetime string the parser was given (if using multiple |
385
|
|
|
|
|
|
|
specifications and an overall I<preprocess> then this is the date after it's |
386
|
|
|
|
|
|
|
been through that preprocessor). |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=item * parsed |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
B<parsed> is the state of parsing so far. Usually empty at this point unless |
391
|
|
|
|
|
|
|
an overall I<preprocess> was given. Items may be placed in it and will be |
392
|
|
|
|
|
|
|
given to any B<postprocess>or and C<< DateTime->new >> (unless the |
393
|
|
|
|
|
|
|
postprocessor deletes it). |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
=item * self, args, label |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
B<self>, B<args>, B<label> as per I<on_match> and I<on_fail>. |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
=back |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
The return value from the routine is what is given to the I<regex>. Note that |
402
|
|
|
|
|
|
|
this is last code stop before the match. |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
B<Note>: mixing I<length> and a I<preprocess> that modifies the length of the |
405
|
|
|
|
|
|
|
input string is probably not what you meant to do. You probably meant to use |
406
|
|
|
|
|
|
|
the I<multiple parser> variant of I<preprocess> which is done B<before> any |
407
|
|
|
|
|
|
|
length calculations. This C<single parser> variant of I<preprocess> is |
408
|
|
|
|
|
|
|
performed B<after> any length calculations. |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
=item * postprocess |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
B<postprocess> is the last code stop before C<< DateTime->new >> is |
413
|
|
|
|
|
|
|
called. It's given the same arguments as I<preprocess>. This allows it to |
414
|
|
|
|
|
|
|
modify the parsed parameters after the parse and before the creation of the |
415
|
|
|
|
|
|
|
object. For example, you might use: |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
{ |
418
|
|
|
|
|
|
|
regex => qr/^(\d\d) (\d\d) (\d\d)$/, |
419
|
|
|
|
|
|
|
params => [qw( year month day )], |
420
|
|
|
|
|
|
|
postprocess => \&_fix_year, |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
|
423
|
|
|
|
|
|
|
where C<_fix_year> is defined as: |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
sub _fix_year { |
426
|
|
|
|
|
|
|
my %args = @_; |
427
|
|
|
|
|
|
|
my ( $date, $p ) = @args{qw( input parsed )}; |
428
|
|
|
|
|
|
|
$p->{year} += $p->{year} > 69 ? 1900 : 2000; |
429
|
|
|
|
|
|
|
return 1; |
430
|
|
|
|
|
|
|
} |
431
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
This will cause the two digit years to be corrected according to the cut |
433
|
|
|
|
|
|
|
off. If the year was '69' or lower, then it is made into 2069 (or 2045, or |
434
|
|
|
|
|
|
|
whatever the year was parsed as). Otherwise it is assumed to be 19xx. The |
435
|
|
|
|
|
|
|
L<DateTime::Format::Mail> module uses code similar to this (only it allows the |
436
|
|
|
|
|
|
|
cut off to be configured and it doesn't use Builder). |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
B<Note>: It is B<very important> to return an explicit value from the |
439
|
|
|
|
|
|
|
I<postprocess> callback. If the return value is false then the parse is taken |
440
|
|
|
|
|
|
|
to have failed. If the return value is true, then the parse is taken to have |
441
|
|
|
|
|
|
|
succeeded and C<< DateTime->new >> is called. |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
=back |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
See the documentation for the individual parsers for their valid keys. |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
Parsers at the time of writing are: |
448
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
=over 4 |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
=item * |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
L<DateTime::Format::Builder::Parser::Regex> - provides regular expression |
454
|
|
|
|
|
|
|
based parsing. |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
=item * |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
L<DateTime::Format::Builder::Parser::Strptime> - provides strptime based |
459
|
|
|
|
|
|
|
parsing. |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
=back |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
=head2 Subroutines / coderefs as specifications. |
464
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
A single parser specification can be a coderef. This was added mostly because |
466
|
|
|
|
|
|
|
it could be and because I knew someone, somewhere, would want to use it. |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
If the specification is a reference to a piece of code, be it a subroutine, |
469
|
|
|
|
|
|
|
anonymous, or whatever, then it's passed more or less straight through. The |
470
|
|
|
|
|
|
|
code should return C<undef> in event of failure (or any false value, but |
471
|
|
|
|
|
|
|
C<undef> is strongly preferred), or a true value in the event of success |
472
|
|
|
|
|
|
|
(ideally a C<DateTime> object or some object that has the same interface). |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
This all said, I generally wouldn't recommend using this feature unless you |
475
|
|
|
|
|
|
|
have to. |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
=head2 Callbacks |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
I mention a number of callbacks in this document. |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
Any time you see a callback being mentioned, you can, if you like, substitute |
482
|
|
|
|
|
|
|
an arrayref of coderefs rather than having the straight coderef. |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=head1 MULTIPLE SPECIFICATIONS |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
These are very easily described as an array of single specifications. |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
Note that if the first element of the array is an arrayref, then you're |
489
|
|
|
|
|
|
|
specifying options. |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
=over 4 |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
=item * preprocess |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
B<preprocess> lets you specify a preprocessor that is called before any of the |
496
|
|
|
|
|
|
|
parsers are tried. This lets you do things like strip off timezones or any |
497
|
|
|
|
|
|
|
unnecessary data. The most common use people have for it at present is to get |
498
|
|
|
|
|
|
|
the input date to a particular length so that the I<length> is usable |
499
|
|
|
|
|
|
|
(L<DateTime::Format::ICal> would use it to strip off the variable length |
500
|
|
|
|
|
|
|
timezone). |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
Arguments are as for the I<single parser> I<preprocess> variant with the |
503
|
|
|
|
|
|
|
exception that I<label> is never given. |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
=item * on_fail |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
B<on_fail> should be a reference to a subroutine that is called if the parser |
508
|
|
|
|
|
|
|
fails. If this is not provided, the default action is to call |
509
|
|
|
|
|
|
|
C<DateTime::Format::Builder::on_fail>, or the C<on_fail> method of the |
510
|
|
|
|
|
|
|
subclass of DTFB that was used to create the parser. |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
=back |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
=head1 EXECUTION FLOW |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
Builder allows you to plug in a fair few callbacks, which can make following |
517
|
|
|
|
|
|
|
how a parse failed (or succeeded unexpectedly) somewhat tricky. |
518
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
=head2 For Single Specifications |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
A single specification will do the following: |
522
|
|
|
|
|
|
|
|
523
|
|
|
|
|
|
|
User calls parser: |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
my $dt = $class->parse_datetime($string); |
526
|
|
|
|
|
|
|
|
527
|
|
|
|
|
|
|
=over 4 |
528
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
=item 1 |
530
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
I<preprocess> is called. It's given C<$string> and a reference to the parsing |
532
|
|
|
|
|
|
|
workspace hash, which we'll call C<$p>. At this point, C<$p> is empty. The |
533
|
|
|
|
|
|
|
return value is used as C<$date> for the rest of this single parser. Anything |
534
|
|
|
|
|
|
|
put in C<$p> is also used for the rest of this single parser. |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
=item 2 |
537
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
I<regex> is applied. |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
=item 3 |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
If I<regex> B<did not> match, then I<on_fail> is called (and is given C<$date> |
543
|
|
|
|
|
|
|
and also I<label> if it was defined). Any return value is ignored and the next |
544
|
|
|
|
|
|
|
thing is for the single parser to return C<undef>. |
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
If I<regex> B<did> match, then I<on_match> is called with the same arguments |
547
|
|
|
|
|
|
|
as would be given to I<on_fail>. The return value is similarly ignored, but we |
548
|
|
|
|
|
|
|
then move to step 4 rather than exiting the parser. |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
=item 4 |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
I<postprocess> is called with C<$date> and a filled out C<$p>. The return |
553
|
|
|
|
|
|
|
value is taken as a indication of whether the parse was a success or not. If |
554
|
|
|
|
|
|
|
it wasn't a success then the single parser will exit at this point, returning |
555
|
|
|
|
|
|
|
undef. |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
=item 5 |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
C<< DateTime->new >> is called and the user is given the resultant C<DateTime> |
560
|
|
|
|
|
|
|
object. |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=back |
563
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
See the section on L<error handling|/"ERROR HANDLING AND BAD PARSES"> |
565
|
|
|
|
|
|
|
regarding the C<undef>s mentioned above. |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
=head2 For Multiple Specifications |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
With multiple specifications: |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
User calls parser: |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
my $dt = $class->complex_parse($string); |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
=over 4 |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
=item 1 |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
The overall I<preprocess>or is called and is given C<$string> and the hashref |
580
|
|
|
|
|
|
|
C<$p> (identically to the per parser I<preprocess> mentioned in the previous |
581
|
|
|
|
|
|
|
flow). |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
If the callback modifies C<$p> then a B<copy> of C<$p> is given to each of the |
584
|
|
|
|
|
|
|
individual parsers. This is so parsers won't accidentally pollute each other's |
585
|
|
|
|
|
|
|
workspace. |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
=item 2 |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
If an appropriate length specific parser is found, then it is called and the |
590
|
|
|
|
|
|
|
single parser flow (see the previous section) is followed, and the parser is |
591
|
|
|
|
|
|
|
given a copy of C<$p> and the return value of the overall I<preprocess>or as |
592
|
|
|
|
|
|
|
C<$date>. |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
If a C<DateTime> object was returned so we go straight back to the user. |
595
|
|
|
|
|
|
|
|
596
|
|
|
|
|
|
|
If no appropriate parser was found, or the parser returned C<undef>, then we |
597
|
|
|
|
|
|
|
progress to step 3! |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=item 3 |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
Any non-I<length> based parsers are tried in the order they were specified. |
602
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
For each of those the single specification flow above is performed, and is |
604
|
|
|
|
|
|
|
given a copy of the output from the overall preprocessor. |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
If a real C<DateTime> object is returned then we exit back to the user. |
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
If no parser could parse, then an error is thrown. |
609
|
|
|
|
|
|
|
|
610
|
|
|
|
|
|
|
=back |
611
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
See the section on L<error handling|/ERROR HANDLING AND BAD PARSES> regarding |
613
|
|
|
|
|
|
|
the C<undef>s mentioned above. |
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
=head1 METHODS |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
In the general course of things you won't need any of the methods. Life often |
618
|
|
|
|
|
|
|
throws unexpected things at us so the methods are all available for use. |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
=head2 import |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
C<import> is a wrapper for C<create_class>. If you specify the I<class> option |
623
|
|
|
|
|
|
|
(see documentation for C<create_class>) it will be ignored. |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=head2 create_class |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
This method can be used as the runtime equivalent of C<import>. That is, it |
628
|
|
|
|
|
|
|
takes the exact same parameters as when one does: |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
use DateTime::Format::Builder ( ... ) |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
That can be (almost) equivalently written as: |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
use DateTime::Format::Builder; |
635
|
|
|
|
|
|
|
DateTime::Format::Builder->create_class( ... ); |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
The difference being that the first is done at compile time while the second |
638
|
|
|
|
|
|
|
is done at run time. |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
In the tutorial I said there were only two parameters at present. I |
641
|
|
|
|
|
|
|
lied. There are actually three of them. |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
=over 4 |
644
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
=item * parsers |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
B<parsers> takes a hashref of methods and their parser specifications. See the |
648
|
|
|
|
|
|
|
L<DateTime::Format::Builder::Tutorial> for details. |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
Note that if you define a subroutine of the same name as one of the methods |
651
|
|
|
|
|
|
|
you define here, an error will be thrown. |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=item * constructor |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
B<constructor> determines whether and how to create a C<new> function in the |
656
|
|
|
|
|
|
|
new class. If given a true value, a constructor is created. If given a false |
657
|
|
|
|
|
|
|
value, one isn't. |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
If given an anonymous sub or a reference to a sub then that is used as |
660
|
|
|
|
|
|
|
C<new>. |
661
|
|
|
|
|
|
|
|
662
|
|
|
|
|
|
|
The default is C<1> (that is, create a constructor using our default code |
663
|
|
|
|
|
|
|
which simply creates a hashref and blesses it). |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
If your class defines its own C<new> method it will not be overwritten. If you |
666
|
|
|
|
|
|
|
define your own C<new> and B<also> tell Builder to define one an error will be |
667
|
|
|
|
|
|
|
thrown. |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
=item * verbose |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
B<verbose> takes a value. If the value is C<undef>, then logging is |
672
|
|
|
|
|
|
|
disabled. If the value is a filehandle then that's where logging will go. If |
673
|
|
|
|
|
|
|
it's a true value, then output will go to C<STDERR>. |
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
Alternatively, call C<$DateTime::Format::Builder::verbose> with the relevant |
676
|
|
|
|
|
|
|
value. Whichever value is given more recently is adhered to. |
677
|
|
|
|
|
|
|
|
678
|
|
|
|
|
|
|
Be aware that verbosity is a global setting. |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
=item * class |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
B<class> is optional and specifies the name of the class in which to create |
683
|
|
|
|
|
|
|
the specified methods. |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
If using this method in the guise of C<import> then this field will cause an |
686
|
|
|
|
|
|
|
error so it is only of use when calling as C<create_class>. |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
=item * version |
689
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
B<version> is also optional and specifies the value to give C<$VERSION> in the |
691
|
|
|
|
|
|
|
class. It's generally not recommended unless you're combining with the |
692
|
|
|
|
|
|
|
I<class> option. A C<ExtUtils::MakeMaker> / C<CPAN> compliant version |
693
|
|
|
|
|
|
|
specification is much better. |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
=back |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
In addition to creating any of the methods it also creates a C<new> method |
698
|
|
|
|
|
|
|
that can instantiate (or clone) objects. |
699
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
=head1 SUBCLASSING |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
In the rest of the documentation I've often lied in order to get some of the |
703
|
|
|
|
|
|
|
ideas across more easily. The thing is, this module's very flexible. You can |
704
|
|
|
|
|
|
|
get markedly different behaviour from simply subclassing it and overriding |
705
|
|
|
|
|
|
|
some methods. |
706
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
=head2 create_method |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
Given a parser coderef, returns a coderef that is suitable to be a method. |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
The default action is to call C<on_fail> in the event of a non-parse, but you |
712
|
|
|
|
|
|
|
can make it do whatever you want. |
713
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
=head2 on_fail |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
This is called in the event of a non-parse (unless you've overridden |
717
|
|
|
|
|
|
|
C<create_method> to do something else. |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
The single argument is the input string. The default action is to call |
720
|
|
|
|
|
|
|
C<croak>. Above, where I've said parsers or methods throw errors, this is |
721
|
|
|
|
|
|
|
the method that is doing the error throwing. |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
You could conceivably override this method to, say, return C<undef>. |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
=head1 USING BUILDER OBJECTS aka USERS USING BUILDER |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
The methods listed in the L<METHODS> section are all you generally need when |
728
|
|
|
|
|
|
|
creating your own class. Sometimes you may not want a full blown class to |
729
|
|
|
|
|
|
|
parse something just for this one program. Some methods are provided to make |
730
|
|
|
|
|
|
|
that task easier. |
731
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
=head2 new |
733
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
The basic constructor. It takes no arguments, merely returns a new |
735
|
|
|
|
|
|
|
C<DateTime::Format::Builder> object. |
736
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
my $parser = DateTime::Format::Builder->new; |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
If called as a method on an object (rather than as a class method), then it |
740
|
|
|
|
|
|
|
clones the object. |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
my $clone = $parser->new; |
743
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
=head2 clone |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
Provided for those who prefer an explicit C<clone> method rather than using |
747
|
|
|
|
|
|
|
C<new> as an object method. |
748
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
my $clone_of_clone = $clone->clone; |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
=head2 parser |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
Given either a single or multiple parser specification, sets the object to |
754
|
|
|
|
|
|
|
have a parser based on that specification. |
755
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
$parser->parser( |
757
|
|
|
|
|
|
|
regex => qr/^ (\d{4}) (\d\d) (\d\d) $/x; |
758
|
|
|
|
|
|
|
params => [qw( year month day )], |
759
|
|
|
|
|
|
|
); |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
The arguments given to C<parser> are handed directly to C<create_parser>. The |
762
|
|
|
|
|
|
|
resultant parser is passed to C<set_parser>. |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
If called as an object method, it returns the object. |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
If called as a class method, it creates a new object, sets its parser and |
767
|
|
|
|
|
|
|
returns that object. |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
=head2 set_parser |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
Sets the parser of the object to the given parser. |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
$parser->set_parser($coderef); |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
Note: this method does not take specifications. It also does not take anything |
776
|
|
|
|
|
|
|
except coderefs. Luckily, coderefs are what most of the other methods produce. |
777
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
The method return value is the object itself. |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
=head2 get_parser |
781
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
Returns the parser the object is using. |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
my $code = $parser->get_parser; |
785
|
|
|
|
|
|
|
|
786
|
|
|
|
|
|
|
=head2 parse_datetime |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
Given a string, it calls the parser and returns the C<DateTime> object that |
789
|
|
|
|
|
|
|
results. |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
my $dt = $parser->parse_datetime('1979 07 16'); |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
The return value, if not a C<DateTime> object, is whatever the parser wants to |
794
|
|
|
|
|
|
|
return. Generally this means that if the parse failed an error will be thrown. |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
=head2 format_datetime |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
If you call this function, it will throw an error. |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
=head1 LONGER EXAMPLES |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
Some longer examples are provided in the distribution. These implement some of |
803
|
|
|
|
|
|
|
the common parsing DateTime modules using Builder. Each of them are, or were, |
804
|
|
|
|
|
|
|
drop in replacements for the modules at the time of writing them. |
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
=head1 THANKS |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
Dave Rolsky (DROLSKY) for kickstarting the DateTime project, writing |
809
|
|
|
|
|
|
|
L<DateTime::Format::ICal> and L<DateTime::Format::MySQL>, and some much needed |
810
|
|
|
|
|
|
|
review. |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
Joshua Hoblitt (JHOBLITT) for the concept, some of the API, impetus for |
813
|
|
|
|
|
|
|
writing the multi-length code (both one length with multiple parsers and |
814
|
|
|
|
|
|
|
single parser with multiple lengths), blame for the Regex custom constructor |
815
|
|
|
|
|
|
|
code, spotting a bug in Dispatch, and more much needed review. |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
Kellan Elliott-McCrea (KELLAN) for even more review, suggestions, |
818
|
|
|
|
|
|
|
L<DateTime::Format::W3CDTF> and the encouragement to rewrite these docs almost |
819
|
|
|
|
|
|
|
100%! |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
Claus Färber (CFAERBER) for having me get around to fixing the |
822
|
|
|
|
|
|
|
auto-constructor writing, providing the 'args'/'self' patch, and suggesting |
823
|
|
|
|
|
|
|
the multi-callbacks. |
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
Rick Measham (RICKM) for L<DateTime::Format::Strptime> which Builder now |
826
|
|
|
|
|
|
|
supports. |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
Matthew McGillis for pointing out that C<on_fail> overriding should be |
829
|
|
|
|
|
|
|
simpler. |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
Simon Cozens (SIMON) for saying it was cool. |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
=head1 SEE ALSO |
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
C<datetime@perl.org> mailing list. |
836
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
http://datetime.perl.org/ |
838
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
L<perl>, L<DateTime>, L<DateTime::Format::Builder::Tutorial>, |
840
|
|
|
|
|
|
|
L<DateTime::Format::Builder::Parser> |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=head1 SUPPORT |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-Format-Builder/issues>. |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
=head1 SOURCE |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
The source code repository for DateTime-Format-Builder can be found at L<https://github.com/houseabsolute/DateTime-Format-Builder>. |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
=head1 DONATIONS |
853
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
If you'd like to thank me for the work I've done on this module, please |
855
|
|
|
|
|
|
|
consider making a "donation" to me via PayPal. I spend a lot of free time |
856
|
|
|
|
|
|
|
creating free software, and would appreciate any support you'd care to offer. |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
Please note that B<I am not suggesting that you must do this> in order for me |
859
|
|
|
|
|
|
|
to continue working on this particular software. I will continue to do so, |
860
|
|
|
|
|
|
|
inasmuch as I have in the past, for as long as it interests me. |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
Similarly, a donation made in this way will probably not make me work on this |
863
|
|
|
|
|
|
|
software much more, unless I get so many donations that I can consider working |
864
|
|
|
|
|
|
|
on free software full time (let's all have a chuckle at that together). |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
To donate, log into PayPal and send money to autarch@urth.org, or use the |
867
|
|
|
|
|
|
|
button at L<https://www.urth.org/fs-donation.html>. |
868
|
|
|
|
|
|
|
|
869
|
|
|
|
|
|
|
=head1 AUTHORS |
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
=over 4 |
872
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
=item * |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
Dave Rolsky <autarch@urth.org> |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
=item * |
878
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
Iain Truskett <spoon@cpan.org> |
880
|
|
|
|
|
|
|
|
881
|
|
|
|
|
|
|
=back |
882
|
|
|
|
|
|
|
|
883
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
884
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
=for stopwords Daisuke Maki James Raspass |
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
=over 4 |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
=item * |
890
|
|
|
|
|
|
|
|
891
|
|
|
|
|
|
|
Daisuke Maki <daisuke@endeworks.jp> |
892
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
=item * |
894
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
James Raspass <jraspass@gmail.com> |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
=back |
898
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
900
|
|
|
|
|
|
|
|
901
|
|
|
|
|
|
|
This software is Copyright (c) 2020 by Dave Rolsky. |
902
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
This is free software, licensed under: |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
The full text of the license can be found in the |
908
|
|
|
|
|
|
|
F<LICENSE> file included with this distribution. |
909
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
=cut |