| 1 | package DateTime::Format::Builder::Parser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 24 | our $VERSION = '0.82'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 24 | use Params::Validate qw( | |||||||||||||||||||||||||||
| 10 | validate SCALAR CODEREF UNDEF ARRAYREF | ||||||||||||||||||||||||||||
| 11 | 24 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 24 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub on_fail { | ||||||||||||||||||||||||||||
| 15 | 4 | ▶ | my ( $self, $input ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
on_fail: called CC=3 SCAR=12.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 4 | my $maker = $self->maker; | |||||||||||||||||||||||||||
| 17 | 4 | ▶ | if ( $maker and $maker->can('on_fail') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$maker and $maker->can("on_fail")
MC/DC0%0/2
$maker and $maker->can("on_fail")
$maker
$maker->can("on_fail")
Truth table33%1/3
A and B
A= $makerB= $maker->can("on_fail")
| |||||||||||||||||||||||||||||
| 18 | 4 | $maker->on_fail($input); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | else { | ||||||||||||||||||||||||||||
| 21 | 0 | croak __PACKAGE__ . ": Invalid date format: $input"; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub no_parser { | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | croak "No parser set for this parser object."; | ||||||||||||||||||||||||||
|
Subroutine
no_parser: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub new { | ||||||||||||||||||||||||||||
| 30 | 42 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 42 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 42 | my $self = bless { | |||||||||||||||||||||||||||
| 34 | on_fail => \&on_fail, | ||||||||||||||||||||||||||||
| 35 | parser => \&no_parser, | ||||||||||||||||||||||||||||
| 36 | }, $class; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 42 | return $self; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | sub maker { $_[0]->{maker} } | ||||||||||||||||||||||||||
|
Subroutine
maker: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub set_maker { | ||||||||||||||||||||||||||||
| 44 | 42 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_maker: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 42 | my $maker = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 42 | $self->{maker} = $maker; | |||||||||||||||||||||||||||
| 48 | weaken $self->{maker} | ||||||||||||||||||||||||||||
| 49 | 42 | ▶ | if ref $self->{maker}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 42 | return $self; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub fail { | ||||||||||||||||||||||||||||
| 55 | 5 | ▶ | my ( $self, $parent, $input ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fail: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 5 | $self->{on_fail}->( $self, $input, $parent ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub parse { | ||||||||||||||||||||||||||||
| 60 | 52 | ▶ | my ( $self, $parent, $input, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 52 | my $r = $self->{parser}->( $parent, $input, @args ); | |||||||||||||||||||||||||||
| 62 | 52 | ▶ | $self->fail( $parent, $input ) unless defined $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 49 | $r; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub set_parser { | ||||||||||||||||||||||||||||
| 67 | 42 | ▶ | my ( $self, $parser ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 42 | $self->{parser} = $parser; | |||||||||||||||||||||||||||
| 69 | 42 | $self; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub set_fail { | ||||||||||||||||||||||||||||
| 73 | 1 | ▶ | my ( $self, $fail ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_fail: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 1 | $self->{on_fail} = $fail; | |||||||||||||||||||||||||||
| 75 | 1 | $self; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 24 | my @callbacks = qw( on_match on_fail postprocess preprocess ); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my %params = ( | ||||||||||||||||||||||||||||
| 83 | common => { | ||||||||||||||||||||||||||||
| 84 | length => { | ||||||||||||||||||||||||||||
| 85 | type => SCALAR | ARRAYREF, | ||||||||||||||||||||||||||||
| 86 | optional => 1, | ||||||||||||||||||||||||||||
| 87 | callbacks => { | ||||||||||||||||||||||||||||
| 88 | 24 | ▶ | 'is an int' => sub { ref $_[0] ? 1 : $_[0] !~ /\D/ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 24 | ▶ | 'not empty' => sub { ref $_[0] ? @{ $_[0] } >= 1 : 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Stuff used by callbacks | ||||||||||||||||||||||||||||
| 94 | label => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||
| 95 | ( | ||||||||||||||||||||||||||||
| 96 | 24 | map { $_ => { type => CODEREF | ARRAYREF, optional => 1 } } | |||||||||||||||||||||||||||
| 97 | @callbacks | ||||||||||||||||||||||||||||
| 98 | ), | ||||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub params { | ||||||||||||||||||||||||||||
| 103 | 67 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
params: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 67 | ▶ | my $caller = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 105 | 67 | return { map {%$_} @params{ $caller, 'common' } }; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 24 | my $all_params; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub params_all { | ||||||||||||||||||||||||||||
| 111 | 67 | ▶ | return $all_params if defined $all_params; | ||||||||||||||||||||||||||
|
Subroutine
params_all: called CC=3 SCAR=11.0
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 16 | my %all_params = map {%$_} values %params; | |||||||||||||||||||||||||||
| 113 | 16 | $_->{optional} = 1 for values %all_params; | |||||||||||||||||||||||||||
| 114 | 16 | $all_params = \%all_params; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 24 | my %inverse; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub valid_params { | ||||||||||||||||||||||||||||
| 120 | 96 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
valid_params: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 96 | my $from = (caller)[0]; | |||||||||||||||||||||||||||
| 122 | 96 | my %args = @_; | |||||||||||||||||||||||||||
| 123 | 96 | $params{$from} = \%args; | |||||||||||||||||||||||||||
| 124 | 96 | for ( keys %args ) { | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # %inverse contains keys matching all the | ||||||||||||||||||||||||||||
| 127 | # possible params; values are the class if and | ||||||||||||||||||||||||||||
| 128 | # only if that class is the only one that uses | ||||||||||||||||||||||||||||
| 129 | # the given param. | ||||||||||||||||||||||||||||
| 130 | 192 | ▶ | $inverse{$_} = exists $inverse{$_} ? undef : $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 96 | undef $all_params; | |||||||||||||||||||||||||||
| 133 | 96 | 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub whose_params { | ||||||||||||||||||||||||||||
| 137 | 77 | ▶ | my $param = shift; | ||||||||||||||||||||||||||
|
Subroutine
whose_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 77 | return $inverse{$param}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 24 | sub create_single_object { | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | my ($self) = shift; | ||||||||||||||||||||||||||
|
Subroutine
create_single_object: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | 0 | my $obj = $self->new; | |||||||||||||||||||||||||||
| 145 | 0 | my $parser = $self->create_single_parser(@_); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | $obj->set_parser($parser); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | sub create_single_parser { | ||||||||||||||||||||||||||||
| 151 | 67 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
create_single_parser: called CC=10 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 67 | ▶ | return $_[0] if ref $_[0] eq 'CODE'; # already code | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 67 | ▶ | @_ = %{ $_[0] } if ref $_[0] eq 'HASH'; # turn hashref into hash | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | # ordinary boring sort | ||||||||||||||||||||||||||||
| 155 | 67 | my %args = validate( @_, params_all() ); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Determine variables for ease of reference. | ||||||||||||||||||||||||||||
| 158 | 67 | for (@callbacks) { | |||||||||||||||||||||||||||
| 159 | 268 | ▶ | $args{$_} = $class->merge_callbacks( $args{$_} ) if $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Determine parser class | ||||||||||||||||||||||||||||
| 163 | 67 | my $from; | |||||||||||||||||||||||||||
| 164 | 67 | for ( keys %args ) { | |||||||||||||||||||||||||||
| 165 | 77 | $from = whose_params($_); | |||||||||||||||||||||||||||
| 166 | 77 | ▶ | next if ( not defined $from ) or ( $from eq 'common' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $from or $from eq "common"
MC/DC50%1/2
not defined $from or $from eq "common"
not defined $from
$from eq "common"
Truth table66%2/3
not(A) or B
A= not defined $fromB= $from eq "common"
| |||||||||||||||||||||||||||||
| 167 | 67 | last; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 67 | ▶ | croak "Could not identify a parsing module to use." unless $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Find and call parser creation method | ||||||||||||||||||||||||||||
| 172 | 67 | ▶ | my $method = $from->can("create_parser") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | or croak | ||||||||||||||||||||||||||||
| 174 | "Can't create a $_ parser (no appropriate create_parser method)"; | ||||||||||||||||||||||||||||
| 175 | 67 | my @args = %args; | |||||||||||||||||||||||||||
| 176 | 67 | %args = validate( @args, $from->params() ); | |||||||||||||||||||||||||||
| 177 | 67 | $from->$method(%args); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | sub merge_callbacks { | ||||||||||||||||||||||||||||
| 181 | 30 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
merge_callbacks: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 30 | ▶ | return unless @_; # No arguments | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 29 | ▶ | return unless $_[0]; # Irrelevant argument | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 28 | my @callbacks = @_; | |||||||||||||||||||||||||||
| 186 | 28 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 27 | ▶ | return $_[0] if ref $_[0] eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 3 | ▶ | @callbacks = @{ $_[0] } if ref $_[0] eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 4 | ▶ | return unless @callbacks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 3 | for (@callbacks) { | |||||||||||||||||||||||||||
| 193 | 15 | ▶ | croak "All callbacks must be coderefs!" unless ref $_ eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | return sub { | ||||||||||||||||||||||||||||
| 197 | 2 | ▶ | my $rv; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 198 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 199 | 2 | for my $cb (@callbacks) { | |||||||||||||||||||||||||||
| 200 | 14 | $rv = $cb->(%args); | |||||||||||||||||||||||||||
| 201 | 14 | ▶ | return $rv unless $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Ugh. Symbiotic. All but postprocessor return the date. | ||||||||||||||||||||||||||||
| 204 | 14 | ▶ | $args{input} = $rv unless $args{parsed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 2 | $rv; | |||||||||||||||||||||||||||
| 207 | 2 | }; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | sub create_multiple_parsers { | ||||||||||||||||||||||||||||
| 211 | 42 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
create_multiple_parsers: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 42 | my ( $options, @specs ) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 42 | my $obj = $class->new; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Organise the specs, and transform them into parsers. | ||||||||||||||||||||||||||||
| 217 | 42 | my ( $lengths, $others ) = $class->sort_parsers( $options, \@specs ); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # Merge callbacks if any. | ||||||||||||||||||||||||||||
| 220 | 42 | for ('preprocess') { | |||||||||||||||||||||||||||
| 221 | $options->{$_} = $class->merge_callbacks( $options->{$_} ) | ||||||||||||||||||||||||||||
| 222 | 42 | ▶ | if $options->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # Custom fail method? | ||||||||||||||||||||||||||||
| 226 | 42 | ▶ | $obj->set_fail( $options->{on_fail} ) if exists $options->{on_fail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Who's our maker? | ||||||||||||||||||||||||||||
| 229 | 42 | ▶ | $obj->set_maker( $options->{maker} ) if exists $options->{maker}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # We don't want to save the whole options hash as a closure, since | ||||||||||||||||||||||||||||
| 232 | # that can cause a circular reference when $options->{maker} is | ||||||||||||||||||||||||||||
| 233 | # set. | ||||||||||||||||||||||||||||
| 234 | 42 | my $preprocess = $options->{preprocess}; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # These are the innards of a multi-parser. | ||||||||||||||||||||||||||||
| 237 | my $parser = sub { | ||||||||||||||||||||||||||||
| 238 | 52 | ▶ | my ( $self, $date, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 239 | 52 | ▶ | return unless defined $date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Parameters common to the callbacks. Pre-prepared. | ||||||||||||||||||||||||||||
| 242 | 52 | ▶ | my %param = ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | self => $self, | ||||||||||||||||||||||||||||
| 244 | ( @args ? ( args => \@args ) : () ), | ||||||||||||||||||||||||||||
| 245 | ); | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 52 | my %p; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Preprocess and potentially fill %p | ||||||||||||||||||||||||||||
| 250 | 52 | ▶ | if ($preprocess) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 7 | $date = $preprocess->( input => $date, parsed => \%p, %param ); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # Find length parser | ||||||||||||||||||||||||||||
| 255 | 52 | ▶ | if (%$lengths) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 9 | my $length = length $date; | |||||||||||||||||||||||||||
| 257 | 9 | my $parser = $lengths->{$length}; | |||||||||||||||||||||||||||
| 258 | 9 | ▶ | if ($parser) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Found one, call it with _copy_ of %p | ||||||||||||||||||||||||||||
| 261 | 8 | my $dt = $parser->( $self, $date, {%p}, @args ); | |||||||||||||||||||||||||||
| 262 | 8 | ▶ | return $dt if defined $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # Or calls all others, with _copy_ of %p | ||||||||||||||||||||||||||||
| 267 | 44 | for my $parser (@$others) { | |||||||||||||||||||||||||||
| 268 | 74 | my $dt = $parser->( $self, $date, {%p}, @args ); | |||||||||||||||||||||||||||
| 269 | 74 | ▶ | return $dt if defined $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # Failed, return undef. | ||||||||||||||||||||||||||||
| 273 | 5 | return; | |||||||||||||||||||||||||||
| 274 | 42 | }; | |||||||||||||||||||||||||||
| 275 | 42 | $obj->set_parser($parser); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | sub sort_parsers { | ||||||||||||||||||||||||||||
| 279 | 42 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
sort_parsers: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 280 | 42 | shift; | |||||||||||||||||||||||||||
| 281 | 42 | my ($specs) = @_; | |||||||||||||||||||||||||||
| 282 | 42 | my ( %lengths, @others ); | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 42 | for my $spec (@$specs) { | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # Put coderefs straight into the 'other' heap. | ||||||||||||||||||||||||||||
| 287 | 67 | ▶ | if ( ref $spec eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 288 | 0 | push @others, $spec; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # Specifications... | ||||||||||||||||||||||||||||
| 292 | elsif ( ref $spec eq 'HASH' ) { | ||||||||||||||||||||||||||||
| 293 | 67 | ▶ | if ( exists $spec->{length} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 12 | my $code = $class->create_single_parser(%$spec); | |||||||||||||||||||||||||||
| 295 | my @lengths | ||||||||||||||||||||||||||||
| 296 | = ref $spec->{length} | ||||||||||||||||||||||||||||
| 297 | 1 | ? @{ $spec->{length} } | |||||||||||||||||||||||||||
| 298 | 12 | ▶ | : ( $spec->{length} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 12 | for my $length (@lengths) { | |||||||||||||||||||||||||||
| 300 | 13 | push @{ $lengths{$length} }, $code; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | else { | ||||||||||||||||||||||||||||
| 304 | 55 | push @others, $class->create_single_parser(%$spec); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # Something else | ||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||
| 310 | 0 | croak "Invalid specification in list."; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 42 | ▶ | while ( my ( $length, $parsers ) = each %lengths ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 9 | $lengths{$length} = $class->chain_parsers($parsers); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 42 | return ( \%lengths, \@others ); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | sub chain_parsers { | ||||||||||||||||||||||||||||
| 322 | 9 | ▶ | my ( $self, $parsers ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
chain_parsers: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 323 | 9 | ▶ | return $parsers->[0] if @$parsers == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | return sub { | ||||||||||||||||||||||||||||
| 325 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 326 | 5 | for my $parser (@$parsers) { | |||||||||||||||||||||||||||
| 327 | 10 | my $rv = $self->$parser(@_); | |||||||||||||||||||||||||||
| 328 | 10 | ▶ | return $rv if defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | 0 | return undef; | |||||||||||||||||||||||||||
| 331 | 3 | }; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | sub create_parser { | ||||||||||||||||||||||||||||
| 335 | 42 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
create_parser: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 336 | 42 | ▶ | if ( not ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # Simple case of single specification as a hash | ||||||||||||||||||||||||||||
| 339 | 0 | return $class->create_single_object(@_); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # Let's see if we were given an options block | ||||||||||||||||||||||||||||
| 343 | 42 | my %options; | |||||||||||||||||||||||||||
| 344 | 42 | ▶ | while ( ref $_[0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 46 | my $options = shift; | |||||||||||||||||||||||||||
| 346 | 46 | %options = ( %options, @$options ); | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # Now, can we create a multi-parser out of the remaining arguments? | ||||||||||||||||||||||||||||
| 350 | 42 | ▶ | if ( ref $_[0] eq 'HASH' or ref $_[0] eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] eq "HASH" or ref $_[0] eq "CODE"
MC/DC50%1/2
ref $_[0] eq "HASH" or ref $_[0] eq "CODE"
ref $_[0] eq "HASH"
ref $_[0] eq "CODE"
Truth table66%2/3
A or B
A= ref $_[0] eq "HASH"B= ref $_[0] eq "CODE"
| |||||||||||||||||||||||||||||
| 351 | 22 | return $class->create_multiple_parsers( \%options, @_ ); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | else { | ||||||||||||||||||||||||||||
| 354 | # If it wasn't a HASH or CODE, then it was (ideally) | ||||||||||||||||||||||||||||
| 355 | # a list of pairs describing a single specification. | ||||||||||||||||||||||||||||
| 356 | 20 | return $class->create_multiple_parsers( \%options, {@_} ); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 24 | require DateTime::Format::Builder::Parser::Dispatch; | |||||||||||||||||||||||||||
| 361 | 24 | require DateTime::Format::Builder::Parser::generic; | |||||||||||||||||||||||||||
| 362 | 24 | require DateTime::Format::Builder::Parser::Quick; | |||||||||||||||||||||||||||
| 363 | 24 | require DateTime::Format::Builder::Parser::Regex; | |||||||||||||||||||||||||||
| 364 | 24 | require DateTime::Format::Builder::Parser::Strptime; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 24 | 1; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # ABSTRACT: Parser creation | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | __END__ | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =pod | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =head1 NAME | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | DateTime::Format::Builder::Parser - Parser creation | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =head1 VERSION | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | version 0.82 | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | my $class = 'DateTime::Format::Builder::Parser'; | ||||||||||||||||||||||||||||
| 387 | my $parser = $class->create_single_parser( %specs ); | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | This is a utility class for L<DateTime::Format::Builder> that | ||||||||||||||||||||||||||||
| 392 | handles creation of parsers. It is to here that C<Builder> delegates | ||||||||||||||||||||||||||||
| 393 | most of its responsibilities. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head1 METHODS | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | There are two sorts of methods in this class. Those used by | ||||||||||||||||||||||||||||
| 398 | parser implementations and those used by C<Builder>. It is | ||||||||||||||||||||||||||||
| 399 | generally unlikely the user will want to use any of them. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | They are presented, grouped according to use. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head2 Parameter Handling (implementations) | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | These methods allow implementations to have validation of | ||||||||||||||||||||||||||||
| 406 | their arguments in a standard manner and due to C<Parser>'s | ||||||||||||||||||||||||||||
| 407 | implementation, these methods also allow C<Parser> to | ||||||||||||||||||||||||||||
| 408 | determine which implementation to use. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =head3 Common parameters | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | These parameters appear for all parser implementations. | ||||||||||||||||||||||||||||
| 413 | These are primarily documented in | ||||||||||||||||||||||||||||
| 414 | L<DateTime::Format::Builder>. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =over 4 | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =item * | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | B<on_match> | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | =item * | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | B<on_fail> | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =item * | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | B<postprocess> | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =item * | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | B<preprocess> | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =item * | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | B<label> | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =item * | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | B<length> may be a number or an arrayref of numbers | ||||||||||||||||||||||||||||
| 441 | indicating the length of the input. This lets us optimize in | ||||||||||||||||||||||||||||
| 442 | the case of static length input. If supplying an arrayref of | ||||||||||||||||||||||||||||
| 443 | numbers, please keep the number of numbers to a minimum. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =back | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head3 params | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | my $params = $self->params(); | ||||||||||||||||||||||||||||
| 450 | validate( @_, $params ); | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Returns declared parameters and C<common> parameters in a hashref | ||||||||||||||||||||||||||||
| 453 | suitable for handing to L<Params::Validate>'s C<validate> function. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =head3 params_all | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | my $all_params = $self->params_all(); | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | Returns a hash of all the valid options. Not recommended | ||||||||||||||||||||||||||||
| 460 | for general use. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head3 valid_params | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | __PACKAGE__->valid_params( %params ); | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Arguments are as per L<Params::Validate>'s C<validate> function. | ||||||||||||||||||||||||||||
| 467 | This method is used to declare what your valid arguments are in | ||||||||||||||||||||||||||||
| 468 | a parser specification. | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =head3 whose_params | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | my $class = whose_params( $key ); | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | Internal function which merely returns to which class a | ||||||||||||||||||||||||||||
| 475 | parameter is unique. If not unique, returns C<undef>. | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =head2 Organizing and Creating Parsers | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =head3 create_single_parser | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | This takes a single specification and returns a coderef that | ||||||||||||||||||||||||||||
| 482 | is a parser that suits that specification. This is the end | ||||||||||||||||||||||||||||
| 483 | of the line for all the parser creation methods. It | ||||||||||||||||||||||||||||
| 484 | delegates no further. | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | If a coderef is specified, then that coderef is immediately | ||||||||||||||||||||||||||||
| 487 | returned (it is assumed to be appropriate). | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | The single specification (if not a coderef) can be either a | ||||||||||||||||||||||||||||
| 490 | hashref or a hash. The keys and values must be as per the | ||||||||||||||||||||||||||||
| 491 | specification. | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | It is here that any arrays of callbacks are unified. It is | ||||||||||||||||||||||||||||
| 494 | also here that any parser implementations are used. With | ||||||||||||||||||||||||||||
| 495 | the spec that's given, the keys are looked at and whichever | ||||||||||||||||||||||||||||
| 496 | module is the first to have a unique key in the spec is the | ||||||||||||||||||||||||||||
| 497 | one to whom the spec is given. | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | B<Note>: please declare a C<valid_params> argument with an | ||||||||||||||||||||||||||||
| 500 | uppercase letter. For example, if you're writing | ||||||||||||||||||||||||||||
| 501 | C<DateTime::Format::Builder::Parser::Fnord>, declare a | ||||||||||||||||||||||||||||
| 502 | parameter called C<Fnord>. Similarly, C<DTFBP::Strptime> | ||||||||||||||||||||||||||||
| 503 | should have C<Strptime> and C<DTFBP::Regex> should have | ||||||||||||||||||||||||||||
| 504 | C<Regex>. These latter two don't for backwards compatibility | ||||||||||||||||||||||||||||
| 505 | reasons. | ||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | The returned parser will return either a C<DateTime> object | ||||||||||||||||||||||||||||
| 508 | or C<undef>. | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head3 merge_callbacks | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Produce either undef or a single coderef from either undef, | ||||||||||||||||||||||||||||
| 513 | an empty array, a single coderef or an array of coderefs | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | =head2 create_multiple_parsers | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | Given the options block (as made from C<create_parser()>) | ||||||||||||||||||||||||||||
| 518 | and a list of single parser specifications, this returns a | ||||||||||||||||||||||||||||
| 519 | coderef that returns either the resultant C<DateTime> object | ||||||||||||||||||||||||||||
| 520 | or C<undef>. | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | It first sorts the specifications using C<sort_parsers()> | ||||||||||||||||||||||||||||
| 523 | and then creates the function based on what that returned. | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | =head2 sort_parsers | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | This takes the list of specifications and sorts them while | ||||||||||||||||||||||||||||
| 528 | turning the specifications into parsers. It returns two | ||||||||||||||||||||||||||||
| 529 | values: the first is a hashref containing all the length | ||||||||||||||||||||||||||||
| 530 | based parsers. The second is an array containing all the | ||||||||||||||||||||||||||||
| 531 | other parsers. | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | If any of the specs are not code or hash references, then it | ||||||||||||||||||||||||||||
| 534 | will call C<croak()>. | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | Code references are put directly into the 'other' array. Any | ||||||||||||||||||||||||||||
| 537 | hash references without I<length> keys are run through | ||||||||||||||||||||||||||||
| 538 | C<create_single_parser()> and the resultant parser is placed | ||||||||||||||||||||||||||||
| 539 | in the 'other' array. | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | Hash references B<with> I<length> keys are run through | ||||||||||||||||||||||||||||
| 542 | C<create_single_parser()>, but the resultant parser is used | ||||||||||||||||||||||||||||
| 543 | as the value in the length hashref with the length being the | ||||||||||||||||||||||||||||
| 544 | key. If two or more parsers have the same I<length> | ||||||||||||||||||||||||||||
| 545 | specified then an error is thrown. | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | =head2 create_parser | ||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||
| 549 | C<create_class()> is mostly a wrapper around | ||||||||||||||||||||||||||||
| 550 | C<create_parser()> that does loops and stuff and calls | ||||||||||||||||||||||||||||
| 551 | C<create_parser()> to create the actual parsers. | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | C<create_parser()> takes the parser specifications (be they | ||||||||||||||||||||||||||||
| 554 | single specifications or multiple specifications) and | ||||||||||||||||||||||||||||
| 555 | returns an anonymous coderef that is suitable for use as a | ||||||||||||||||||||||||||||
| 556 | method. The coderef will call C<croak()> in the event of | ||||||||||||||||||||||||||||
| 557 | being unable to parse the single string it expects as input. | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | The simplest input is that of a single specification, | ||||||||||||||||||||||||||||
| 560 | presented just as a plain hash, not a hashref. This is | ||||||||||||||||||||||||||||
| 561 | passed directly to C<create_single_parser()> with the return | ||||||||||||||||||||||||||||
| 562 | value from that being wrapped in a function that lets it | ||||||||||||||||||||||||||||
| 563 | C<croak()> on failure, with that wrapper being returned. | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | If the first argument to C<create_parser()> is an arrayref, | ||||||||||||||||||||||||||||
| 566 | then that is taken to be an options block (as per the | ||||||||||||||||||||||||||||
| 567 | multiple parser specification documented earlier). | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | Any further arguments should be either hashrefs or coderefs. | ||||||||||||||||||||||||||||
| 570 | If the first argument after the optional arrayref is not a | ||||||||||||||||||||||||||||
| 571 | hashref or coderef then that argument and all remaining | ||||||||||||||||||||||||||||
| 572 | arguments are passed off to C<create_single_parser()> | ||||||||||||||||||||||||||||
| 573 | directly. If the first argument is a hashref or coderef, | ||||||||||||||||||||||||||||
| 574 | then it and the remaining arguments are passed to | ||||||||||||||||||||||||||||
| 575 | C<create_multiple_parsers()>. | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | The resultant coderef from calling either of the creation | ||||||||||||||||||||||||||||
| 578 | methods is then wrapped in a function that calls C<croak()> | ||||||||||||||||||||||||||||
| 579 | in event of failure or the C<DateTime> object in event of | ||||||||||||||||||||||||||||
| 580 | success. | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | =head1 FINDING IMPLEMENTATIONS | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | C<Parser> automatically loads any parser classes in C<@INC>. | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | To be loaded automatically, you must be a | ||||||||||||||||||||||||||||
| 587 | C<DateTime::Format::Builder::Parser::XXX> module. | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | To be invisible, and not loaded, start your class with a lower class | ||||||||||||||||||||||||||||
| 590 | letter. These are ignored. | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =head1 WRITING A PARSER IMPLEMENTATION | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | =head2 Naming your parser | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | Create a module and name it in the form | ||||||||||||||||||||||||||||
| 597 | C<DateTime::Format::Builder::Parser::XXX> | ||||||||||||||||||||||||||||
| 598 | where I<XXX> is whatever you like, | ||||||||||||||||||||||||||||
| 599 | so long as it doesn't start with a | ||||||||||||||||||||||||||||
| 600 | lower case letter. | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | Alternatively, call it something completely different | ||||||||||||||||||||||||||||
| 603 | if you don't mind the users explicitly loading your module. | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | I'd recommend keeping within the C<DateTime::Format::Builder> | ||||||||||||||||||||||||||||
| 606 | namespace though --- at the time of writing I've not given | ||||||||||||||||||||||||||||
| 607 | thought to what non-auto loaded ones should be called. Any | ||||||||||||||||||||||||||||
| 608 | ideas, please email me. | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =head2 Declaring specification arguments | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | Call C<<DateTime::Format::Builder::Parser->valid_params()>> with | ||||||||||||||||||||||||||||
| 613 | C<Params::Validate> style arguments. For example: | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | DateTime::Format::Builder::Parser->valid_params( | ||||||||||||||||||||||||||||
| 616 | params => { type => ARRAYREF }, | ||||||||||||||||||||||||||||
| 617 | Regex => { type => SCALARREF, callbacks => { | ||||||||||||||||||||||||||||
| 618 | 'is a regex' => sub { ref(shift) eq 'Regexp' } | ||||||||||||||||||||||||||||
| 619 | }} | ||||||||||||||||||||||||||||
| 620 | ); | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | Start one of the key names with a capital letter. Ideally that key | ||||||||||||||||||||||||||||
| 623 | should match the I<XXX> from earlier. This will be used to help | ||||||||||||||||||||||||||||
| 624 | identify which module a parser specification should be given to. | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | The key names I<on_match>, I<on_fail>, I<postprocess>, I<preprocess>, | ||||||||||||||||||||||||||||
| 627 | I<label> and I<length> are predefined. You are recommended to make use | ||||||||||||||||||||||||||||
| 628 | of them. You may ignore I<length> as C<sort_parsers> takes care of that. | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head2 Define create_parser | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | A class method of the name C<create_parser> that does the following: | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | Its arguments are as for a normal method (i.e. class as first argument). | ||||||||||||||||||||||||||||
| 635 | The other arguments are the result from a call to C<Params::Validate> | ||||||||||||||||||||||||||||
| 636 | according to your specification (the C<valid_params> earlier), i.e. a | ||||||||||||||||||||||||||||
| 637 | hash of argument name and value. | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | The return value should be a coderef that takes a date string as its | ||||||||||||||||||||||||||||
| 640 | first argument and returns either a C<DateTime> object or C<undef>. | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | =head2 Callbacks | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | It is preferred that you support some callbacks to your parsers. | ||||||||||||||||||||||||||||
| 645 | In particular, C<preprocess>, C<on_match>, C<on_fail> and | ||||||||||||||||||||||||||||
| 646 | C<postprocess>. See the L<main Builder|DateTime::Format::Builder> | ||||||||||||||||||||||||||||
| 647 | docs for the appropriate placing of calls to the callbacks. | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | C<datetime@perl.org> mailing list. | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | http://datetime.perl.org/ | ||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | L<perl>, L<DateTime>, L<DateTime::Format::Builder>. | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | L<Params::Validate>. | ||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | L<DateTime::Format::Builder::Parser::generic>, | ||||||||||||||||||||||||||||
| 660 | L<DateTime::Format::Builder::Parser::Dispatch>, | ||||||||||||||||||||||||||||
| 661 | L<DateTime::Format::Builder::Parser::Quick>, | ||||||||||||||||||||||||||||
| 662 | L<DateTime::Format::Builder::Parser::Regex>, | ||||||||||||||||||||||||||||
| 663 | L<DateTime::Format::Builder::Parser::Strptime>. | ||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | Bugs may be submitted at L<http://rt.cpan.org/Public/Dist/Display.html?Name=DateTime-Format-Builder> or via email to L<bug-datetime-format-builder@rt.cpan.org|mailto:bug-datetime-format-builder@rt.cpan.org>. | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | =head1 SOURCE | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | The source code repository for DateTime-Format-Builder can be found at L<https://github.com/houseabsolute/DateTime-Format-Builder>. | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | =over 4 | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =item * | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | =item * | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | Iain Truskett | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | =back | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | This software is Copyright (c) 2019 by Dave Rolsky. | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 698 | F<LICENSE> file included with this distribution. | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | =cut | ||||||||||||||||||||||||||||