• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/CPANInternal-140/DateTime-Format-Builder/lib/DateTime/Format/Builder/Parser/
1package DateTime::Format::Builder::Parser::Quick;
2use strict;
3use vars qw( $VERSION %dispatch_data );
4use Params::Validate qw( SCALAR OBJECT CODEREF validate );
5use base qw( DateTime::Format::Builder::Parser );
6
7=head1 NAME
8
9DateTime::Format::Builder::Parser::Quick - Use another formatter, simply
10
11=head1 SYNOPSIS
12
13    use DateTime::Format::Builder (
14    parsers => { parse_datetime => [
15        { Quick => 'DateTime::Format::HTTP' },
16        { Quick => 'DateTime::Format::Mail' },
17        { Quick => 'DateTime::Format::IBeat' },
18    ]});
19
20is the same as:
21
22    use DateTime::Format::HTTP;
23    use DateTime::Format::Mail;
24    use DateTime::Format::IBeat;
25
26    use DateTime::Format::Builder (
27    parsers => { parse_datetime => [
28        sub { eval { DateTime::Format::HTTP->parse_datetime( $_[1] ) } },
29        sub { eval { DateTime::Format::Mail->parse_datetime( $_[1] ) } },
30        sub { eval { DateTime::Format::IBeat->parse_datetime( $_[1] ) } },
31    ]});
32
33(These two pieces of code can both be found in the test
34suite; one as F<quick.t>, the other as F<fall.t>.)
35
36=head1 DESCRIPTION
37
38C<Quick> adds a parser that allows some shortcuts when
39writing fairly standard and mundane calls to other
40formatting modules.
41
42=head1 SPECIFICATION
43
44C<Quick> has two keys, one optional.
45
46The C<Quick> keyword should have an argument of either an
47object or a class name. If it's a class name then the class
48is C<use>d.
49
50The C<method> keyword is optional with a default of
51C<parse_datetime>. It's either name of the method to invoke
52on the object, or a reference to a piece of code.
53
54In any case, the resultant code ends up looking like:
55
56     my $rv = $Quick->$method( $date );
57
58=cut
59
60$VERSION = '0.77';
61
62__PACKAGE__->valid_params(
63    Quick => {
64	type => SCALAR|OBJECT,
65        callbacks => {
66            good_classname => sub {
67                (ref $_[0]) or ( $_[0] =~ /^\w+[:'\w+]*\w+/ )
68            },
69        }
70    },
71    method => {
72        optional => 1,
73        type => SCALAR|CODEREF,
74    },
75);
76
77sub create_parser
78{
79    my ($self, %args) = @_;
80    my $class = $args{Quick};
81    my $method = $args{method};
82    $method = 'parse_datetime' unless defined $method;
83    eval "use $class";
84    die $@ if $@;
85
86    return sub {
87        my ($self, $date) = @_;
88	return unless defined $date;
89        my $rv = eval { $class->$method( $date ) };
90        return $rv if defined $rv;
91	return;
92    };
93}
94
951;
96
97__END__
98
99=head1 THANKS
100
101See L<the main module's section|DateTime::Format::Builder/"THANKS">.
102
103=head1 SUPPORT
104
105Support for this module is provided via the datetime@perl.org email
106list. See http://lists.perl.org/ for more details.
107
108Alternatively, log them via the CPAN RT system via the web or email:
109
110    http://perl.dellah.org/rt/dtbuilder
111    bug-datetime-format-builder@rt.cpan.org
112
113This makes it much easier for me to track things and thus means
114your problem is less likely to be neglected.
115
116=head1 LICENCE AND COPYRIGHT
117
118Copyright E<copy> Iain Truskett, 2003. All rights reserved.
119
120This library is free software; you can redistribute it and/or modify
121it under the same terms as Perl itself, either Perl version 5.000 or,
122at your option, any later version of Perl 5 you may have available.
123
124The full text of the licences can be found in the F<Artistic> and
125F<COPYING> files included with this module, or in L<perlartistic> and
126L<perlgpl> as supplied with Perl 5.8.1 and later.
127
128=head1 AUTHOR
129
130Iain Truskett <spoon@cpan.org>
131
132=head1 SEE ALSO
133
134C<datetime@perl.org> mailing list.
135
136http://datetime.perl.org/
137
138L<perl>, L<DateTime>,
139L<DateTime::Format::Builder>
140
141=cut
142
143
144