Mooseのアトリビュートには多くのプロパティがあります。アトリビュートはおそらくMooseの機能としてはずば抜けて強力で柔軟なものですし、アトリビュートを宣言するだけで強力なクラスを作れます。実際、アトリビュートの宣言しかないクラスを作ることもできます。 アトリビュートは、あるクラスに属するすべてのメンバーが持つプロパティです。たとえば、「Personオブジェクトにはかならず姓と名がある」といえます。また、アトリビュートは省略可能にすることもできるので、「Personオブジェクトの中には社会保障番号を持つものもある(持たないものもある)」ということもできます。 もっとも単純な例では、アトリビュートは(ハッシュのように)読み書き可能な名前付きの値とも考えられます。ただし、アトリビュートの場合はデフォルト値や型制約があったり、委譲などを行うこともできます。 アトリビュートは、ほかの言語ではス
Mooseにはさまざまな機能がありますし、使い方も決して一通りではありません。でも、使う機能は一部にしぼって、いつもそれを使うようにした方が、みなさんのためになると思います。 もちろんどんな「ベストプラクティス」集でもそうですが、これも本当にただの意見にすぎませんので、無視していただいても結構です。 Mooseのクラス定義の最後にはMooseのシュガー関数を削除してクラスを不変化することをおすすめします。 package Person; use Moose; # extends, roles, attributes, etc. # methods no Moose; __PACKAGE__->meta->make_immutable; 1; no Mooseの部分はMooseがエクスポートするキーワードを全て取り払う、単にコードをきれいにするためのものです。クラスを定義した後にこれらのキー
Mooseはアトリビュートのために独自の型システムを用意しています。また、MooseXモジュールの助けを借りるとこれらの型をメソッドのパラメータの検証に使うこともできます。 Mooseの型システムのもとになったのは、Perl 5自身の「暗黙の」型と、Perl 6のいくつかのコンセプトを組み合わせたものです。独自の制約を使うと自前のサブタイプを簡単に作れるため、どのような種類の検証コードでも簡単に表現できます。 型には名前がついているので、名指しで再利用できます。そのため、大きなアプリケーション全体で型を共有することも簡単にできます。 ただし、ここで明確にしておきたいのは、これは「本当の」型システムではないということ。Mooseを使うと魔法の力でPerlが型と変数を関連づけるようになるわけではありません。これは単に名前と制約を関連づけられる高度なパラメータチェックシステムにすぎないのです。
Moose Quick-Ref Card A modern object system for Perl 5 Exported Functions use Moose; Turns on strict and warnings. Exports confess and blessed. extends @superclasses Moose’s alternative to use base. Note that it will re-set @ISA. with @roles with $role => { %options } Consume roles (interfaces) as an alternative to extending classes. has $name => %options Install an attribute into this class. See
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く