call methods on this object, such as
the datetime method, which returns a
friendly string representing the date
and time.
Delegations
Alternatively, you could utilize Moose’s
delegation feature when you set up
the fired_dt attribute, like this:
has 'fired_dt' => ( is => 'rw', isa => 'DateTime', handles => { last_fired => 'datetime' } );
This will set up another accessor
method named last_fired and map
it to the datetime method of the
attribute. This makes the invocations
of $self->last_fired and $self->fired_dt->datetime
equivalent. This is worthwhile because it
allows you to keep your API simpler.
Types
Moose provides its own type system for
enforcing constraints on the value to
which an attribute can be set. As I
mentioned earlier, type constraints are
set with the isa attribute option.
Moose provides a built-in hierarchy of
named types for general-purpose use.
130 | SEPTEMBER 2011 WWW.LINUXJOURNAL.COM
For example, Int is a subtype of Num,
and Num is a subtype of Str. The value 'foo' would pass Str but not Num or
Int; 3. 4 would pass Str and Num but
not Int, and 7 would pass all of Str,
Num and Int.