=encoding utf8 =pod =head1 題名 Moose::Cookbook::Snack::Types - 型や型制約を利用するときのサンプルコード =head1 概要 package Point; use Moose; has 'x' => ( isa => 'Int', is => 'ro' ); has 'y' => ( isa => 'Int', is => 'rw' ); package main; my $point = eval { Point->new( x => 'fifty', y => 'forty' ); }; if ($@) { print "Oops: $@"; } my $point; my $xval = 'forty-two'; my $xattribute = Point->meta->find_attribute_by_name('x'); my $xtype_constraint = $xattribute->type_constraint; if ( $xtype_constraint->check($xval) ) { $point = Point->new( x => $xval, y => 0 ); } else { print "Value: $xval is not an " . $xtype_constraint->name . "\n"; } =head1 本文 これはLで利用したPointの例に型チェックを追加したものです。 Cなアトリビュートに文字列の値を割り当てようとすると、Mooseは明示的にエラーメッセージを出して死にます。エラーメッセージにはアトリビュート名と型制約名、制約チェックに失敗した値が含まれています。 ここではCを使ってエラーメッセージをC<$@>に取り込んでいます。 後半では、LからLオブジェクトを取得して、Lを利用して直接値のチェックを行っています。 =head1 参照 =over 4 =item L =item L =item L =back =head1 作者 Jess Robinson =head1 COPYRIGHT AND LICENSE Copyright 2006-2009 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut