- 追加された行はこの色です。
- 削除された行はこの色です。
* Moose 基本 [#gc25e183]
* 基本 (コンストラクタ/継承/型)[#gc25e183]
** コンストラクタ [#wcc12752]
sub BUILD {
my ($self, $args) = @_;
}
new()は使わない。
** 継承 [#f6bc135c]
package Dog;
use Moose;
extends 'Animal';
** subtypeによる型のカスタマイズ[#f172c1a4]
** typeによる型の作成 [#b6b14d1c]
package Obj;
use Moose;
use Moose::Util::TypeConstraints;
use Time::Piece;
type 'Obj::Time::Piece' => where { defined $_ ? $_->isa('Time::Piece') : 1 };
has tm => (
is => 'rw',
isa => 'Obj::Time::Piece',
);
package main;
use Data::Dumper;
use Time::Piece;
use DateTime;
my $tp = Time::Piece->new;
my $dt = DateTime->now;
my $t1 = Obj->new( tm => $tp ); # OK
my $t2 = Obj->new( tm => undef ); # OK
my $t3 = Obj->new( tm => $dt ); # Error!
- Obj::Time::Piece型はTime::PieceかUndefだけ通す。
** subtypeによる型のカスタマイズ(親クラスの型の継承)[#f172c1a4]
package Obj;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'Obj::Int'
=> as 'Int'
=> where { $_ >= 10 }
=> message { "$_ is under 10" };
has n => (
is => 'rw',
isa => 'Obj::Int',
);
package main;
my $o = Obj->new;
$o->n(9);
subtypeを使うとMaybe[]が使えなくなる。
- subtypeはtypeと違って親クラスを継承する。
- subtypeを使うとMaybe[]が使えなくなる。
** coerceによる型の強制変換 [#g40d4f31]
package Obj;
use Mouse;
use Mouse::Util::TypeConstraints;
coerce 'Int'
=> from 'Str'
=> via { int $_ };
=> from 'Str'
=> via { 1 }
=> from 'Undef'
=> via { 0 };
has n => (
is => 'rw',
isa => 'Int',
coerce => 1,
is => 'rw',
isa => 'Int',
coerce => 1,
);
has()にcoerce => 1を指定すること。
package main;
my $o = Obj->new;
$o->n('abc');
print $o->n,"\n";
print $o->n,"\n"; # 1
$o->n(undef);
print $o->n,"\n"; # 0
- hasでcoerce => 1を指定すると、hasの型はIntなので、coerce 'Int'が呼び出される。
- fromでhas nに渡した引数の型を指定し、via でどのような型変換をするか記述する。
- なお、hasに渡した引数の型がすでにIntならcoerceは呼び出されない。
*** 参考 [#mb444186]
- http://search.cpan.org/perldoc?Moose::Util::TypeConstraints
- http://search.cpan.org/perldoc?Moose::Cookbook::Basics::Recipe5
** subypeとcoerceを使ってDateTimeアトリビュートを実装 [#o36bcbe0]
package Obj;
use Mouse;
use Mouse::Util::TypeConstraints;
use DateTime::Format::Pg;
subtype 'Obj::DateTime'
=> as 'DateTime';
coerce 'Obj::DateTime'
=> from 'Str'
=> via { DateTime::Format::Pg->parse_date($_) };
=> from 'Undef'
=> via { DateTime->now };
has dt => (
is => 'rw',
isa => 'Obj::DateTime',
coerce => 1,
);
package main;
my $o = Obj->new;
$o->dt('2009-02-03');
print $o->dt->ymd,"\n";
** aroundによるゲッターの拡張 [#r170c3d1]
package Obj;
use Moose;
has n => (
is => 'rw',
isa => 'Int',
);
around 'n' => sub {
my $next = shift;
my $self = shift;
return $self->$next . '!' unless @_;
my $arg = shift;
return $self->$next($arg);
};
package main;
my $o = Obj->new;
$o->n(10);
print $o->n, "\n"; # 10!とビックリがつく
http://search.cpan.org/perldoc?Moose::Manual::FAQ#How_can_I_inflate/deflate_values_in_accessors?
** メソッド・アトリビュート一覧を取得する [#q40b13fb]
package Obj;
use Moose;
has n => (
is => 'rw',
);
package main;
use Data::Dumper;
my $o = Obj->new;
print Dumper $o->get_attribute_list;
print Dumper $o->get_method_list;
- Mouse(Moose)::Meta::Classを使う。
- http://search.cpan.org/perldoc?Mouse::Meta::Class
** 参考 [#p5c69fa6]
- http://perl-mongers.org/2010/02/the-fastest-way-to-mastering-moose-and-mouse.html