Skip to content

Instantly share code, notes, and snippets.

@jleader
Created January 26, 2012 00:26
Show Gist options
  • Save jleader/1679986 to your computer and use it in GitHub Desktop.
Save jleader/1679986 to your computer and use it in GitHub Desktop.
package MyObject;
use Moose;
use Moose::Util::TypeConstraints;
use MooseX::Types::DateTime;
use DateTime::Format::ISO8601;
use namespace::autoclean;
subtype 'DateTime8601',
as 'MooseX::Types::DateTime';
use Data::Dumper;
coerce 'DateTime8601',
from 'Str',
via {
$_ = DateTime::Format::ISO8601->parse_datetime($_);
};
has 'CreationDate' => ( is => 'ro', isa => 'DateTime8601', coerce => 1 );
__PACKAGE__->meta->make_immutable;
1;
#!/usr/bin/perl
use strict;
use warnings;
use MyObject;
use Data::Dumper;
my $x = MyObject->new(CreationDate => '2011-09-02T18:22:05.526Z');
print Dumper($x);
Attribute (CreationDate) does not pass the type constraint because: Validation failed for 'DateTime8601' with value DateTime=HASH(0x25cc9f0) at constructor MyObject::new (defined at MyObject.pm line 20) line 35
MyObject::new('MyObject', 'CreationDate', '2011-09-02T18:22:05.526Z') called at ./myscript.pl line 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment