Moose Moose - It's the new camel #18

What if you want to change the date

package Time;

use Moose;
use Moose::Util::TypeConstraints;

use DateTime;

subtype 'DateTime'
    => as Object
    => where { $_->isa('DateTime') };

coerce 'DateTime'
    => from 'HashRef'
        => via { DateTime->new( %{ $_ } ) }
    => from Int
        => via { DateTime->from_epoch(epoch => $_) };

has 'time' => (
    is      => 'rw',
    isa     => 'DateTime',
    coerce  => 1,
    default => sub {
        DateTime->now();
    }
);

And to use it:

package main;

my $d = Time->new();

$d->time({year => 2005, month => 7, day => 4});

print $d->time()->ymd . "\n";

$d->time(time);

print $d->time()->ymd . "\n";
Copyright © 2007 Robert Boone