| 1 | package DateTime::Format::Builder::Parser::generic; | ||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 24 | use Params::Validate qw( | |||||||||||||||||||||||||||
| 10 | validate SCALAR CODEREF UNDEF | ||||||||||||||||||||||||||||
| 11 | 24 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | 65 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 65 | bless {@_}, $class; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub generic_parser { | ||||||||||||||||||||||||||||
| 19 | 65 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
generic_parser: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | my %args = validate( | ||||||||||||||||||||||||||||
| 21 | @_, | ||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||
| 23 | ( | ||||||||||||||||||||||||||||
| 24 | 65 | map { $_ => { type => CODEREF, optional => 1 } } | |||||||||||||||||||||||||||
| 25 | qw( | ||||||||||||||||||||||||||||
| 26 | on_match on_fail preprocess postprocess | ||||||||||||||||||||||||||||
| 27 | ) | ||||||||||||||||||||||||||||
| 28 | ), | ||||||||||||||||||||||||||||
| 29 | label => { type => SCALAR | UNDEF, optional => 1 }, | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | 65 | my $label = $args{label}; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | my $callback | ||||||||||||||||||||||||||||
| 35 | 65 | ▶ | = ( exists $args{on_match} or exists $args{on_fail} ) ? 1 : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $args{"on_match"} or exists $args{"on_fail"}
MC/DC50%1/2
exists $args{"on_match"} or exists $args{"on_fail"}
exists $args{"on_match"}
exists $args{"on_fail"}
Truth table66%2/3
A or B
A= exists $args{"on_match"}B= exists $args{"on_fail"}
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | return sub { | ||||||||||||||||||||||||||||
| 38 | 84 | ▶ | my ( $self, $date, $p, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=27.6
| |||||||||||||||||||||||||||||
| 39 | 84 | ▶ | return unless defined $date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 84 | my %p; | |||||||||||||||||||||||||||
| 41 | 84 | ▶ | %p = %$p if $p; # Look! A Copy! | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 84 | ▶ | my %param = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | self => $self, | ||||||||||||||||||||||||||||
| 45 | ( defined $label ? ( label => $label ) : () ), | ||||||||||||||||||||||||||||
| 46 | ( @args ? ( args => \@args ) : () ), | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Preprocess - can modify $date and fill %p | ||||||||||||||||||||||||||||
| 50 | 84 | ▶ | if ( $args{preprocess} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | $date = $args{preprocess} | ||||||||||||||||||||||||||||
| 52 | 8 | ->( input => $date, parsed => \%p, %param ); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 84 | ▶ | my $rv = $class->do_match( $date, @args ) if $class->can('do_match'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Funky callback thing | ||||||||||||||||||||||||||||
| 58 | 84 | ▶ | if ($callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 6 | ▶ | my $type = defined $rv ? "on_match" : "on_fail"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | $args{$type}->( input => $date, %param ) if $args{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 84 | ▶ | return unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 44 | my $dt; | |||||||||||||||||||||||||||
| 65 | 44 | ▶ | $dt = $class->post_match( $date, $rv, \%p ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | if $class->can('post_match'); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Allow post processing. Return undef if regarded as failure | ||||||||||||||||||||||||||||
| 69 | 44 | ▶ | if ( $args{postprocess} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 10 | my $rv = $args{postprocess}->( | |||||||||||||||||||||||||||
| 71 | parsed => \%p, | ||||||||||||||||||||||||||||
| 72 | input => $date, | ||||||||||||||||||||||||||||
| 73 | post => $dt, | ||||||||||||||||||||||||||||
| 74 | %param, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | 10 | ▶ | return unless $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # A successful match! | ||||||||||||||||||||||||||||
| 80 | 44 | ▶ | $dt = $class->make( $date, $dt, \%p ) if $class->can('make'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 44 | return $dt; | |||||||||||||||||||||||||||
| 82 | 65 | }; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 24 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 24 | for (qw( valid_params params )) { | |||||||||||||||||||||||||||
| 88 | 48 | *$_ = *{"DateTime::Format::Builder::Parser::$_"}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 24 | 1; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # ABSTRACT: Useful routines | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =pod | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 NAME | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | DateTime::Format::Builder::Parser::generic - Useful routines | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 VERSION | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | version 0.82 | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 METHODS | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 Useful | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head3 new | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Standard constructor. Returns a blessed hash; any arguments are placed | ||||||||||||||||||||||||||||
| 117 | in the hash. This is useful for storing information between methods. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head3 generic_parser | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | This is a method provided solely for the benefit of | ||||||||||||||||||||||||||||
| 122 | C<Parser> implementations. It semi-neatly abstracts | ||||||||||||||||||||||||||||
| 123 | a lot of the work involved. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Basically, it takes parameters matching the assorted | ||||||||||||||||||||||||||||
| 126 | callbacks from the parser declarations and makes a coderef | ||||||||||||||||||||||||||||
| 127 | out of it all. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Currently recognized callbacks are: | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =over 4 | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =item * | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | on_match | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item * | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | on_fail | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =item * | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | preprocess | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item * | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | postprocess | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 Methods for subclassing | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | These are methods you should define when writing your own subclass. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | B<Note>: these methods do not exist in this class. There is no point | ||||||||||||||||||||||||||||
| 156 | trying to call C<< $self->SUPER::do_match( ... ) >>. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head3 do_match | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | C<do_match> is the first phase. Arguments are the date and @args. | ||||||||||||||||||||||||||||
| 161 | C<self>, C<label>, C<args>. Return value must be defined if you match | ||||||||||||||||||||||||||||
| 162 | successfully. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head3 post_match | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | C<post_match> is called after the appropriate callback out of | ||||||||||||||||||||||||||||
| 167 | C<on_match>/C<on_fail> is done. It's passed the date, the return | ||||||||||||||||||||||||||||
| 168 | value from C<do_match> and the parsing hash. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Its return value is used as the C<post> argument to the C<postprocess> | ||||||||||||||||||||||||||||
| 171 | callback, and as the second argument to C<make>. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head3 make | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | C<make> takes the original input, the return value from C<post_match> | ||||||||||||||||||||||||||||
| 176 | and the parsing hash and should return a C<DateTime> object or | ||||||||||||||||||||||||||||
| 177 | undefined. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 Delegations | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | For use of C<Parser>, this module also delegates C<valid_params> and | ||||||||||||||||||||||||||||
| 182 | C<params>. This is just convenience to save typing the following: | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | DateTime::Format::Builder::Parser->valid_params( blah ) | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Instead we get to type: | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | $self->valid_params( blah ); | ||||||||||||||||||||||||||||
| 189 | __PACKAGE__->valid_params( blah ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 WRITING A SUBCLASS | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Rather than attempt to explain how it all works, I think it's best if | ||||||||||||||||||||||||||||
| 194 | you take a look at F<Regex.pm> and F<Strptime.pm> as examples and | ||||||||||||||||||||||||||||
| 195 | work from there. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | C<datetime@perl.org> mailing list. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | http://datetime.perl.org/ | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | L<perl>, L<DateTime>, L<DateTime::Format::Builder>, | ||||||||||||||||||||||||||||
| 204 | L<DateTime::Format::Builder::Parser>. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 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>. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 SOURCE | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | The source code repository for DateTime-Format-Builder can be found at L<https://github.com/houseabsolute/DateTime-Format-Builder>. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =over 4 | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item * | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Dave Rolsky <autarch@urth.org> | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =item * | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | Iain Truskett | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =back | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | This software is Copyright (c) 2019 by Dave Rolsky. | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 239 | F<LICENSE> file included with this distribution. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||