Blame lib/DateTime/Format/Builder/Parser/Regex.pm

Packit 9002b2
package DateTime::Format::Builder::Parser::Regex;
Packit 9002b2
{
Packit 9002b2
  $DateTime::Format::Builder::Parser::Regex::VERSION = '0.81';
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
Packit 9002b2
use strict;
Packit 9002b2
use warnings;
Packit 9002b2
use vars qw( @ISA );
Packit 9002b2
use Params::Validate qw( validate ARRAYREF SCALARREF HASHREF CODEREF );
Packit 9002b2
Packit 9002b2
use DateTime::Format::Builder::Parser::generic;
Packit 9002b2
@ISA = qw( DateTime::Format::Builder::Parser::generic );
Packit 9002b2
Packit 9002b2
__PACKAGE__->valid_params(
Packit 9002b2
Packit 9002b2
    # How to match
Packit 9002b2
    params => {
Packit 9002b2
        type => ARRAYREF,    # mapping $1,$2,... to new() args
Packit 9002b2
    },
Packit 9002b2
    regex => {
Packit 9002b2
        type      => SCALARREF,
Packit 9002b2
        callbacks => {
Packit 9002b2
            'is a regex' => sub { ref(shift) eq 'Regexp' }
Packit 9002b2
        }
Packit 9002b2
    },
Packit 9002b2
Packit 9002b2
    # How to create
Packit 9002b2
    extra => {
Packit 9002b2
        type     => HASHREF,
Packit 9002b2
        optional => 1,
Packit 9002b2
    },
Packit 9002b2
    constructor => {
Packit 9002b2
        type      => CODEREF | ARRAYREF,
Packit 9002b2
        optional  => 1,
Packit 9002b2
        callbacks => {
Packit 9002b2
            'array has 2 elements' => sub {
Packit 9002b2
                ref( $_[0] ) eq 'ARRAY' ? ( @{ $_[0] } == 2 ) : 1;
Packit 9002b2
                }
Packit 9002b2
        }
Packit 9002b2
    },
Packit 9002b2
);
Packit 9002b2
Packit 9002b2
sub do_match {
Packit 9002b2
    my $self    = shift;
Packit 9002b2
    my $date    = shift;
Packit 9002b2
    my @matches = $date =~ $self->{regex};
Packit 9002b2
    return @matches ? \@matches : undef;
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
sub post_match {
Packit 9002b2
    my $self = shift;
Packit 9002b2
    my ( $date, $matches, $p ) = @_;
Packit 9002b2
Packit 9002b2
    # Fill %p from match
Packit 9002b2
    @{$p}{ @{ $self->{params} } } = @$matches;
Packit 9002b2
    return;
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
sub make {
Packit 9002b2
    my $self = shift;
Packit 9002b2
    my ( $date, $dt, $p ) = @_;
Packit 9002b2
    my @args = ( %$p, %{ $self->{extra} } );
Packit 9002b2
    if ( my $cons = $self->{constructor} ) {
Packit 9002b2
        if ( ref $cons eq 'ARRAY' ) {
Packit 9002b2
            my ( $class, $method ) = @$cons;
Packit 9002b2
            return $class->$method(@args);
Packit 9002b2
        }
Packit 9002b2
        elsif ( ref $cons eq 'CODE' ) {
Packit 9002b2
            return $self->$cons(@args);
Packit 9002b2
        }
Packit 9002b2
    }
Packit 9002b2
    else {
Packit 9002b2
        return DateTime->new(@args);
Packit 9002b2
    }
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
sub create_parser {
Packit 9002b2
    my ( $self, %args ) = @_;
Packit 9002b2
    $args{extra} ||= {};
Packit 9002b2
    unless ( ref $self ) {
Packit 9002b2
        $self = $self->new(%args);
Packit 9002b2
    }
Packit 9002b2
Packit 9002b2
    # Create our parser
Packit 9002b2
    return $self->generic_parser(
Packit 9002b2
        (
Packit 9002b2
            map { exists $args{$_} ? ( $_ => $args{$_} ) : () }
Packit 9002b2
                qw(
Packit 9002b2
                on_match on_fail preprocess postprocess
Packit 9002b2
                )
Packit 9002b2
        ),
Packit 9002b2
        label => $args{label},
Packit 9002b2
    );
Packit 9002b2
}
Packit 9002b2
Packit 9002b2
1;
Packit 9002b2
Packit 9002b2
# ABSTRACT: Regex based date parsing
Packit 9002b2
Packit 9002b2
__END__
Packit 9002b2
Packit 9002b2
=pod
Packit 9002b2
Packit 9002b2
=head1 NAME
Packit 9002b2
Packit 9002b2
DateTime::Format::Builder::Parser::Regex - Regex based date parsing
Packit 9002b2
Packit 9002b2
=head1 VERSION
Packit 9002b2
Packit 9002b2
version 0.81
Packit 9002b2
Packit 9002b2
=head1 SYNOPSIS
Packit 9002b2
Packit 9002b2
   my $parser = DateTime::Format::Builder->create_parser(
Packit 9002b2
	regex  => qr/^(\d\d\d\d)(\d\d)(\d\d)T(\d\d)(\d\d)(\d\d)$/,
Packit 9002b2
	params => [ qw( year month day hour minute second ) ],
Packit 9002b2
   );
Packit 9002b2
Packit 9002b2
=head1 SPECIFICATION
Packit 9002b2
Packit 9002b2
In addition to the
Packit 9002b2
L<common keys|DateTime::Format::Builder/"SINGLE SPECIFICATIONS">,
Packit 9002b2
C<Regex> supports:
Packit 9002b2
Packit 9002b2
=over 4
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
B<regex> is a regular expression that should capture
Packit 9002b2
elements of the datetime string.
Packit 9002b2
This is a required element. This is the key whose presence
Packit 9002b2
indicates it's a specification that belongs to this class.
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
B<params> is an arrayref of key names. The captures from the
Packit 9002b2
regex are mapped to these (C<$1> to the first element, C<$2>
Packit 9002b2
to the second, and so on) and handed to
Packit 9002b2
C<< DateTime->new() >>.
Packit 9002b2
This is a required element.
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
B<extra> is a hashref of extra arguments you wish to give to
Packit 9002b2
C<< DateTime->new() >>. For example, you could set the
Packit 9002b2
C<year> or C<time_zone> to defaults:
Packit 9002b2
Packit 9002b2
    extra => { year => 2004, time_zone => "Australia/Sydney" },
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
B<constructor> is either an arrayref or a coderef. If an arrayref
Packit 9002b2
then the first element is a class name or object, and the second
Packit 9002b2
element is a method name (or coderef since Perl allows that sort of
Packit 9002b2
thing).  The arguments to the call are anything in C<$p> and
Packit 9002b2
anything given in the C<extra> option above.
Packit 9002b2
Packit 9002b2
If only a coderef is supplied, then it is called with arguments of
Packit 9002b2
C<$self>, C<$p> and C<extra>.
Packit 9002b2
Packit 9002b2
In short:
Packit 9002b2
Packit 9002b2
            $self->$coderef( %$p, %{ $self->{extra} } );
Packit 9002b2
Packit 9002b2
The method is expected to return a valid L<DateTime> object,
Packit 9002b2
or undef in event of failure, but can conceivably return anything
Packit 9002b2
it likes. So long as it's 'true'.
Packit 9002b2
Packit 9002b2
=back
Packit 9002b2
Packit 9002b2
=head1 SUPPORT
Packit 9002b2
Packit 9002b2
See L<DateTime::Format::Builder> for details.
Packit 9002b2
Packit 9002b2
=head1 SEE ALSO
Packit 9002b2
Packit 9002b2
C<datetime@perl.org> mailing list.
Packit 9002b2
Packit 9002b2
http://datetime.perl.org/
Packit 9002b2
Packit 9002b2
L<perl>, L<DateTime>,
Packit 9002b2
L<DateTime::Format::Builder>
Packit 9002b2
Packit 9002b2
=head1 AUTHORS
Packit 9002b2
Packit 9002b2
=over 4
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
Dave Rolsky <autarch@urth.org>
Packit 9002b2
Packit 9002b2
=item *
Packit 9002b2
Packit 9002b2
Iain Truskett
Packit 9002b2
Packit 9002b2
=back
Packit 9002b2
Packit 9002b2
=head1 COPYRIGHT AND LICENSE
Packit 9002b2
Packit 9002b2
This software is Copyright (c) 2013 by Dave Rolsky.
Packit 9002b2
Packit 9002b2
This is free software, licensed under:
Packit 9002b2
Packit 9002b2
  The Artistic License 2.0 (GPL Compatible)
Packit 9002b2
Packit 9002b2
=cut