DateTime::Format::Builder::Parser::Dispatch - Dispatch parsers by group


DateTime-Format-Builder documentation Contained in the DateTime-Format-Builder distribution.

Index


Code Index:

NAME

Top

DateTime::Format::Builder::Parser::Dispatch - Dispatch parsers by group

SYNOPSIS

Top

    package SampleDispatch;
    use DateTime::Format::Builder
    (
	parsers => {
	    parse_datetime => [
		{
		    Dispatch => sub {
			return 'fnerk';
		    }
		}
	    ]
	},
	groups => {
	    fnerk => [
		{
		    regex => qr/^(\d{4})(\d\d)(\d\d)$/,
		    params => [qw( year month day )],
		},
	    ]
	}
    );

DESCRIPTION

Top

Dispatch adds another parser type to Builder permitting dispatch of parsing according to group names.

SPECIFICATION

Top

Dispatch has just one key: Dispatch. The value should be a reference to a subroutine that returns one of:

Groups are specified much like the example in the SYNOPSIS. They follow the same format as when you specify them for methods.

SIDEEFFECTS

Top

Your group parser can also be a Dispatch parser. Thus you could potentially end up with an infinitely recursive parser.

THANKS

Top

See the main module's section.

SUPPORT

Top

Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details.

Alternatively, log them via the CPAN RT system via the web or email:

    http://perl.dellah.org/rt/dtbuilder
    bug-datetime-format-builder@rt.cpan.org

This makes it much easier for me to track things and thus means your problem is less likely to be neglected.

LICENCE AND COPYRIGHT

Top

AUTHOR

Top

Iain Truskett <spoon@cpan.org>

SEE ALSO

Top

datetime@perl.org mailing list.

http://datetime.perl.org/

perl, DateTime, DateTime::Format::Builder


DateTime-Format-Builder documentation Contained in the DateTime-Format-Builder distribution.
package DateTime::Format::Builder::Parser::Dispatch;
use strict;
use vars qw( $VERSION %dispatch_data );
use Params::Validate qw( CODEREF validate );
use DateTime::Format::Builder::Parser;

$VERSION = '0.78';

{
    no strict 'refs';
    *dispatch_data = *DateTime::Format::Builder::dispatch_data;
    *params = *DateTime::Format::Builder::Parser::params;
}

DateTime::Format::Builder::Parser->valid_params(
    Dispatch => {
	type => CODEREF,
    }
);

sub create_parser
{
    my ($self, %args) = @_;
    my $coderef = $args{Dispatch};

    return sub {
	my ($self, $date, $p, @args) = @_;
	return unless defined $date;
	my $class = ref($self)||$self;

	my @results = $coderef->( $date );
	return unless @results;
	return unless defined $results[0];

	for my $group (@results)
	{
	    my $parser = $dispatch_data{$class}{$group};
	    die "Unknown parsing group: $class\n" unless defined $parser;
	    my $rv = eval { $parser->parse( $self, $date, $p, @args ) };
	    return $rv unless $@ or not defined $rv;
	}
	return;
    };
}

1;

__END__