DateTime::Format::Builder::Tutorial 0.7807 review

Download
by rbytes.net on

DateTime::Format::Builder::Tutorial is a quick class on using Builder. CREATING A CLASS As most people who are writing modules

License: Perl Artistic License
File size: 47K
Developer: Iain Truskett
0 stars award from rbytes.net

DateTime::Format::Builder::Tutorial is a quick class on using Builder.

CREATING A CLASS

As most people who are writing modules know, you start a package with a package declaration and some indication of module version:

package DateTime::Format::ICal;
our $VERSION = '0.04';

After that, you call Builder with some options. There are only a few (detailed later). Right now, we're only interested in parsers.

use DateTime::Format::Builder
(
parsers => {
...
}
);

The parsers option takes a reference to a hash of method names and specifications:

parsers => {
parse_datetime => ... ,
parse_datetime_with_timezone => ... ,
...
}

Builder will create methods in your class, each method being a parser that follows the given specifications. It is strongly recommended that one method is called parse_datetime, be it a Builder created method or one of your own.

In addition to creating any of the parser methods it also creates a new() method that can instantiate (or clone) objects of this class. This behaviour can be modified with the constructor option, but we don't need to know that yet.

Each value corresponding to a method name in the parsers list is either a single specification, or a list of specifications. We'll start with the simple case.

parse_briefdate => {
params => [ qw( year month day ) ],
regex => qr/^(dddd)(dd)(dd)$/,
},

This will result in a method named parse_briefdate which will take strings in the form 20040716 and return DateTime objects representing that date. A user of the class might write:

use DateTime::Format::ICal;
my $date = "19790716";
my $dt = DateTime::Format::ICal->parse_briefdate( $date );
print "My birth month is ", $dt->month_name, "n";

The regex is applied to the input string, and if it matches, then $1, $2, ... are mapped to the params given and handed to DateTime->new(). Essentially:

my $rv = DateTime->new( year => $1, month => $2, day => $3 );

There are more complicated things one can do within a single specification, but we'll cover those later.

Often, you'll want a method to be able to take one string, and run it against multiple parser specifications. It would be very irritating if the user had to work out what format the datetime string was in and then which method was most appropriate.

So, Builder lets you specify multiple specifications:

parse_datetime => [
{
params => [ qw( year month day hour minute second ) ],
regex => qr/^(dddd)(dd)(dd)T(dd)(dd)(dd)$/,
},
{
params => [ qw( year month day hour minute ) ],
regex => qr/^(dddd)(dd)(dd)T(dd)(dd)$/,
},
{
params => [ qw( year month day hour ) ],
regex => qr/^(dddd)(dd)(dd)T(dd)$/,
},
{
params => [ qw( year month day ) ],
regex => qr/^(dddd)(dd)(dd)$/,
},
],

It's an arrayref of specifications. A parser will be created that will try each of these specifications sequentially, in the order you specified.

Requirements:
Perl

DateTime::Format::Builder::Tutorial 0.7807 keywords