#! /usr/bin/perl use warnings; use strict; use Time::Piece; { package My; use Moo; use Time::Piece; sub is_date { my ($date) = @_; die "Wrong type" unless shift->isa('Time::Piece'); } use namespace::clean; has time_piece => (is => 'rw', isa => \&is_date); has string_date => ( is => 'rw', coerce => sub { my $tp = shift; $tp = 'Time::Piece'->strptime($tp, '%Y%m%d') unless $tp->isa('Time::Piece'); return $tp }, isa => \&is_date); } my $now = localtime; my $o = 'My'->new( time_piece => $now, string_date => '20170911' ); eval { $o->string_date($now) } or warn "1: $@"; # works eval { $o->string_date('a') } or warn "2: $@"; # coercion for "s +tring_date" failed: Error parsing time eval { $o->time_piece('20170911') } or warn "3: $@"; # isa check for " +time_piece" failed: Wrong type
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
In reply to Re: Moo's coerce and isa difference
by choroba
in thread Moo's coerce and isa difference
by krautcat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |