=encoding utf-8 =head1 NAME =begin original Class::Accessor::Lite - a minimalistic variant of Class::Accessor =end original Class::Accessor::Lite - Class::Accessorの最小機能版 =head1 SYNOPSIS package MyPackage; use Class::Accessor::Lite ( new => 1, rw => [ qw(foo bar) ], ro => [ qw(baz) ], wo => [ qw(hoge) ], ); =head1 DESCRIPTION =begin original The module is a variant of C. It is fast and requires less typing, has no dependencies to other modules, and does not mess up the @ISA. =end original このモジュールは Class::Accessorの亜種です. 高速でタイプ数が少なく済み, 他のモジュールの依存もなく, @ISAを汚すこともありません. =head1 THE USE STATEMENT =begin original The use statement (i.e. the C function) of the module takes a single hash as an argument that specifies the types and the names of the properties. Recognises the following keys. =over 4 =item new => $true_or_false the default constructor is created if the value evaluates to true, otherwise nothing is done (the default behaviour) =item rw => \@name_of_the_properties creates a read / write accessor for the name of the properties passed through as an arrayref =item ro => \@name_of_the_properties creates a write-only accessor for the name of the properties passed through as an arrayref =item rw => \@name_of_the_properties creates a read-only accessor for the name of the properties passed through as an arrayref =back For more detailed explanation read the following section describing the behaviour of each function that actually creates the accessors. =end original モジュールの use文(すなわち C関数)はプロパティの種類と名前を示すハッシュを一つ受け取ります. 以下に示すキーを認識します. =over 4 =item new => $true_or_false この値が真であるとき, デフォルトコンストラクタが作成され, その他の場合は何も作成されません(デフォルト動作) =item rw => \@name_of_the_properties 配列リファレンスとして渡されたプロパティの名前の読み取り/書き込みアクセサを作成します. =item ro => \@name_of_the_properties 配列リファレンスとして渡されたプロパティの名前の読み取りのみのアクセサを作成します. =item wo => \@name_of_the_properties 配列リファレンスとして渡されたプロパティの名前の書き込みのみのアクセサを作成します. =back 実際にアクセサを作るそれぞれの関数の振る舞いについて述べた以下のセクションにより詳細な説明があります. =head1 FUNCTIONS =begin original As of version 0.04 the properties can be specified as the arguments to the C statement (as can be seen in the SYNOPSIS) which is now the recommended way of using the module, but for compatibility the following functions are provided as well. =head2 Class::Accessor::Lite->mk_accessors(@name_of_the_properties) Creates an accessor in current package under the name specified by the arguments that access the properties (of a hashref) with the same name. =head2 Class::Accessor::Lite->mk_ro_accessors(@name_of_the_properties) Same as mk_accessors() except it will generate read-only accessors (i.e. true accessors). If you attempt to set a value with these accessors it will throw an exception. =head2 Class::Accessor::Lite->mk_wo_accessors(@name_of_the_properties) Same as mk_accessors() except it will generate write-only accessors (i.e. mutators). If you attempt to read a value with these accessors it will throw an exception. =head2 Class::Accessor::Lite->mk_new() Creates the C function that accepts a hash or a hashref as the initial properties of the object. =head2 Class::Accessor::Lite->mk_new_and_accessors(@name_of_the_properties) DEPRECATED. Use the new "use Class::Accessor::Lite (...)" style. =end original バージョン 0.04では, プロパティは C文の引数として指定することができます(SYNOPSISで見れます), これは現在は推奨される方法です. ただし互換性のために以下の関数も同様に提供されます. =head2 Class::Accessor::Lite->mk_accessors(@name_of_the_properties) カレントパッケージ下に引数で指定された名前のアクセサを作成します. アクセサは同じ名前の(ハッシュリファレンスの)プロパティにアクセスします. =head2 Class::Accessor::Lite->mk_ro_accessors(@name_of_the_properties) 読み出しのみのアクセサを生成することを除き, mk_accessors()と同じです(すなわち純粋なアクセサ). もし生成されたアクセサを用いて値の設定を行なった場合, 例外が投げられます. =head2 Class::Accessor::Lite->mk_wo_accessors(@name_of_the_properties) 書き込みのみのアクセサを生成することを除き, mk_accessors()と同じです(すなわち書き換え可能). もし生成されたアクセサを用いて値を読みだそうとすると, 例外が投げられます. =head2 Class::Accessor::Lite->mk_new() オブジェクトのプロパティを初期化する C関数を作成します. new関数は引数としてハッシュもしくはハッシュリファレンス受け取ります. =head2 Class::Accessor::Lite->mk_new_and_accessors(@name_of_the_properties) DEPRECATED. Use the new "use Class::Accessor::Lite (...)" style. 廃止予定. new "use Class::Accessor::Lite (...)"スタイルの使用. =head1 FAQ =begin original =head2 Can I use C in an inherited module? Yes in most cases, when the class object in the super class is implemeted using a hashref. However you _should_ _not_ create the constructor for the inherited class by calling Cnew()> or by C 1). The only other thing that C does is to set up the accessor functions for given property names through a blessed hashref. =head2 What happens when passing more than one arguments to the accessor? When the accessor built by Class::Accessor::Lite is given more than one arguments, a reference to the arguments will be saved as an arrayref. This behaviour might not be necessary but is implemented as is to maintain compatibility with L. my @data = (1, 2, 3); $obj->someproperty(@data); $obj->someproperty->[2]++; # $data[3] is incremented In general, you should pass an arrayref to set an arrayref to a property. my @data = (1, 2, 3); $obj->someproperty([ @data ]); # save a copy using arrayref $obj->someproper->[2]++; # @data is not modified =end original =head2 Cを継承されたモジュール内で利用したいのですが ? スーパークラスがハッシュリファレンスを使って実装されているのであれば, 多くの場合は可能です. しかし継承したクラスのコンストラクタを Cnew()>もしくは C 1)を使って作成するべきではありません. Cが与えられた名前について blessされたハッシュリファレンスにアクセスするための, アクセサをセットアップするだけにすべきです. =head2 アクセサに一つより多いの引数が渡された場合はどうなるのですか ? Class::Accessor::Liteに作成されたアクセサに一つより多い引数が渡されたとき, 引数のリファレンスが配列リファレンスとして格納されます. この振る舞いは必要ではないと考えられますが, Lとの互換性を維持するために実装しています. my @data = (1, 2, 3); $obj->someproperty(@data); $obj->someproperty->[2]++; # $data[3]がインクリメントされてしまいます. 一般的に, プロパティに配列リファレンスをセットするために, 配列リファレンスを渡すべきです. my @data = (1, 2, 3); $obj->someproperty([ @data ]); # 使用する配列リファレンスのコピーが格納されます. $obj->someproper->[2]++; # @dataは変更されません. =head1 SEE ALSO L L =head1 AUTHORS Copyright (C) 2008 - 2010 Kazuho Oku =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available. =cut