<?xml version='1.0' encoding='utf-8'?>
<pod xmlns="http://axkit.org/ns/2000/pod2xml">
<head>
	<title>perlbot - Bag'o Object Tricks (the BOT)</title>
</head>
<sect1>
<title>perlbot - Bag'o Object Tricks (the BOT)</title>
<para>
perlbot - Bag'o Object Tricks (the BOT)
</para>
</sect1>
<sect1>
<title>DESCRIPTION</title>
<para>
The following collection of tricks and hints is intended to whet curious
appetites about such things as the use of instance variables and the
mechanics of object and class relationships.  The reader is encouraged to
consult relevant textbooks for discussion of Object Oriented definitions and
methodology.  This is not intended as a tutorial for object-oriented
programming or as a comprehensive guide to Perl's object oriented features,
nor should it be construed as a style guide.  If you're looking for tutorials,
be sure to read <link xref='perlboot'>perlboot</link>, <link xref='perltoot'>perltoot</link>, and <link xref='perltooc'>perltooc</link>.
</para>
<para>
ここにあるインスタンス変数の使用であるとか、オブジェクトやクラス関係の
機構といったトリックやヒントのコレクションは、好奇心を刺激するようなものです。
読者は、オブジェクト指向の定義や方法論についての議論については
適切な教科書を参照することが求められます。
つまり、これはオブジェクト指向プログラミングのチュートリアルを
指向したものでなく、Perl のオブジェクト指向機能についての
包括的なガイドでもなく、はたまたスタイルガイドとなるべきものでもありません。
</para>
<para>
The Perl motto still holds:  There's more than one way to do it.
</para>
<para>
Perlのモットーはまだ生きています: やり方は一通りじゃない。
</para>
</sect1>
<sect1>
<title>OO SCALING TIPS</title>
<list>
<item><itemtext>1</itemtext>
<para>
Do not attempt to verify the type of $self.  That'll break if the class is
inherited, when the type of $self is valid but its package isn't what you
expect.  See rule 5.
</para>
<para>
$self の型を確かめることはしないこと。
$self の型が正当であってもそのパッケージがあなたの期待しているものと
異なっているとき、クラスが継承されていればおかしな結果となってしまいます。
規則 5 を参照してください。
</para>
</item>
<item><itemtext>2</itemtext>
<para>
If an object-oriented (OO) or indirect-object (IO) syntax was used, then the
object is probably the correct type and there's no need to become paranoid
about it.  Perl isn't a paranoid language anyway.  If people subvert the OO
or IO syntax then they probably know what they're doing and you should let
them do it.  See rule 1.
</para>
<para>
オブジェクト指向 (OO) あるいは 間接オブジェクト (IO) 構文が使われたならば、
そのオブジェクトはおそらくは正しい型であり、
それについて偏執的になる必要はありません。
どうせ Perl は偏執的言語ではありません。
OO 構文や IO 構文をくつがえす人々がいたなら、そういった人々はおそらく
自分たちが行っていることを知っているでしょうし、
あなたは彼らにそうさせておくべきなのです。
規則 1 を参照してください。
</para>
</item>
<item><itemtext>3</itemtext>
<para>
Use the two-argument form of bless().  Let a subclass use your constructor.
See <link xref='INHERITING A CONSTRUCTOR'>INHERITING A CONSTRUCTOR</link>.
</para>
<para>
引数 2 つの bless() を使いましょう。
コンストラクタを使ってサブクラスにしましょう。
<link xref='INHERITING A CONSTRUCTOR'>INHERITING A CONSTRUCTOR</link> を参照してください。
</para>
</item>
<item><itemtext>4</itemtext>
<para>
The subclass is allowed to know things about its immediate superclass, the
superclass is allowed to know nothing about a subclass.
</para>
<para>
サブクラスは、すぐ上のスーパークラスが何であるかを知ることが許されています。
スーパークラスはサブクラスについての何かを知ることを許されていません。
</para>
</item>
<item><itemtext>5</itemtext>
<para>
Don't be trigger happy with inheritance.  A &quot;using&quot;, &quot;containing&quot;, or
&quot;delegation&quot; relationship (some sort of aggregation, at least) is often more
appropriate.  See <link xref='OBJECT RELATIONSHIPS'>OBJECT RELATIONSHIPS</link>, <link xref='USING RELATIONSHIP WITH SDBM'>USING RELATIONSHIP WITH SDBM</link>,
and <link xref='#DELEGATION'>&quot;DELEGATION&quot;</link>.
</para>
<para>
やたらと継承を使わないようにしましょう。
実装(&quot;using&quot;)、包含(&quot;containing&quot;)、委任(&quot;delegation&quot;)といった
関係(少なくともある種の集合)は、たいていの場合継承よりも
適切なものとなります。
<link xref='OBJECT RELATIONSHIPS'>OBJECT RELATIONSHIPS</link>, <link xref='USING RELATIONSHIP WITH SDBM'>USING RELATIONSHIP WITH SDBM</link>,
<link xref='#DELEGATION'>&quot;DELEGATION&quot;</link> を参照してください。
</para>
</item>
<item><itemtext>6</itemtext>
<para>
The object is the namespace.  Make package globals accessible via the
object.  This will remove the guess work about the symbol's home package.
See <link xref='CLASS CONTEXT AND THE OBJECT'>CLASS CONTEXT AND THE OBJECT</link>.
</para>
<para>
オブジェクトとは名前空間です。
オブジェクトを通して、パッケージを大域的にアクセスできるようにします。
これはシンボルのホームパッケージに関する当て推量を取り除くでしょう。
<link xref='CLASS CONTEXT AND THE OBJECT'>CLASS CONTEXT AND THE OBJECT</link> を参照してください。
</para>
</item>
<item><itemtext>7</itemtext>
<para>
IO syntax is certainly less noisy, but it is also prone to ambiguities that
can cause difficult-to-find bugs.  Allow people to use the sure-thing OO
syntax, even if you don't like it.
</para>
<para>
IO 構文はそれほどややこしいものではありません。
しかしその反面、見つけるのが難しいバグを引き起こすあいまいさになる
傾向があります。
あなたが好まないとしても、人々は OO 構文を使うことが許されているのです。
</para>
</item>
<item><itemtext>8</itemtext>
<para>
Do not use function-call syntax on a method.  You're going to be bitten
someday.  Someone might move that method into a superclass and your code
will be broken.  On top of that you're feeding the paranoia in rule 2.
</para>
<para>
メソッドを関数呼び出し形式で使わないようにしましょう。
さもなければあなたはいつの日か痛い目にあうこととなるでしょう。
誰かがそのようなメソッドをスーパークラスに移動させればあなたのプログラムは
壊れてしまうのです。
規則 2 の偏執狂になることに繋がりかねません。
</para>
</item>
<item><itemtext>9</itemtext>
<para>
Don't assume you know the home package of a method.  You're making it
difficult for someone to override that method.  See <link xref='THINKING OF CODE REUSE'>THINKING OF CODE REUSE</link>.
</para>
<para>
自分がメソッドのホームパッケージを知っていると仮定しないでください。
それをやってしまうと、他の人がそのメソッドをオーバーライドするのが
難しくなってしまいます。
<link xref='THINKING OF CODE REUSE'>THINKING OF CODE REUSE</link> を参照してください。
</para>
</item>
</list>
</sect1>
<sect1>
<title>INSTANCE VARIABLES</title>
<para>
(インスタンス変数)
</para>
<para>
An anonymous array or anonymous hash can be used to hold instance
variables.  Named parameters are also demonstrated.
</para>
<para>
無名配列や無名ハッシュはインスタンス変数を保持するのに使うことができます。
名前付きのパラメータと一緒にお見せしましょう。
</para>
<verbatim><![CDATA[
package Foo;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my %params = @_;
	my $self = {};
	$self->{'High'} = $params{'High'};
	$self->{'Low'}  = $params{'Low'};
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package Bar;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my %params = @_;
	my $self = [];
	$self->[0] = $params{'Left'};
	$self->[1] = $params{'Right'};
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = Foo->new( 'High' => 42, 'Low' => 11 );
print "High=$a->{'High'}\n";
print "Low=$a->{'Low'}\n";
]]></verbatim>
<verbatim><![CDATA[
$b = Bar->new( 'Left' => 78, 'Right' => 40 );
print "Left=$b->[0]\n";
print "Right=$b->[1]\n";
]]></verbatim>
</sect1>
<sect1>
<title>SCALAR INSTANCE VARIABLES</title>
<para>
(スカラインスタンス変数)
</para>
<para>
An anonymous scalar can be used when only one instance variable is needed.
</para>
<para>
無名のスカラは、インスタンス変数が一つだけ必要とされるときのみ
使うことができます。
</para>
<verbatim><![CDATA[
package Foo;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self;
	$self = shift;
	bless \$self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = Foo->new( 42 );
print "a=$$a\n";
]]></verbatim>
</sect1>
<sect1>
<title>INSTANCE VARIABLE INHERITANCE</title>
<para>
(インスタンス変数の継承)
</para>
<para>
This example demonstrates how one might inherit instance variables from a
superclass for inclusion in the new class.  This requires calling the
superclass's constructor and adding one's own instance variables to the new
object.
</para>
<para>
次の例は、どのようにして新しいクラスにスーパークラスからインスタンス変数を
継承するのかということを示すものです。
ここでは、スーパークラスのコンストラクタを呼び出すことと新しい
オブジェクトでインスタンス変数を一つ追加するということを行っています。
</para>
<verbatim><![CDATA[
package Bar;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = {};
	$self->{'buz'} = 42;
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package Foo;
@ISA = qw( Bar );
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = Bar->new;
	$self->{'biz'} = 11;
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = Foo->new;
print "buz = ", $a->{'buz'}, "\n";
print "biz = ", $a->{'biz'}, "\n";
]]></verbatim>
</sect1>
<sect1>
<title>OBJECT RELATIONSHIPS</title>
<para>
(オブジェクトの関係)
</para>
<para>
The following demonstrates how one might implement &quot;containing&quot; and &quot;using&quot;
relationships between objects.
</para>
<para>
以下の例では、包含(&quot;containing&quot;)や実装(&quot;using&quot;)といったオブジェクト間の
関係をどのように実装するかということを説明しています。
</para>
<verbatim><![CDATA[
package Bar;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = {};
	$self->{'buz'} = 42;
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package Foo;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = {};
	$self->{'Bar'} = Bar->new;
	$self->{'biz'} = 11;
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = Foo->new;
print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
print "biz = ", $a->{'biz'}, "\n";
]]></verbatim>
</sect1>
<sect1>
<title>OVERRIDING SUPERCLASS METHODS</title>
<para>
(スーパークラスのメソッドをオーバーライドする)
</para>
<para>
The following example demonstrates how to override a superclass method and
then call the overridden method.  The <strong>SUPER</strong> pseudo-class allows the
programmer to call an overridden superclass method without actually knowing
where that method is defined.
</para>
<para>
以下の例ではどのようにスーパークラスのメソッドをオーバーライドするのか、
そしてオーバーライドされたメソッドをどのように呼び出すのかを示します。
擬似クラス <strong>SUPER</strong> はプログラマがスーパークラスのメソッドを
そのメソッドがどのクラスに属するかを知らなくても呼べるようにします。
</para>
<verbatim><![CDATA[
package Buz;
sub goo { print "here's the goo\n" }
]]></verbatim>
<verbatim><![CDATA[
package Bar; @ISA = qw( Buz );
sub google { print "google here\n" }
]]></verbatim>
<verbatim><![CDATA[
package Baz;
sub mumble { print "mumbling\n" }
]]></verbatim>
<verbatim><![CDATA[
package Foo;
@ISA = qw( Bar Baz );
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	bless [], $type;
}
sub grr { print "grumble\n" }
sub goo {
	my $self = shift;
	$self->SUPER::goo();
}
sub mumble {
	my $self = shift;
	$self->SUPER::mumble();
}
sub google {
	my $self = shift;
	$self->SUPER::google();
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$foo = Foo->new;
$foo->mumble;
$foo->grr;
$foo->goo;
$foo->google;
]]></verbatim>
<para>
Note that <code>SUPER</code> refers to the superclasses of the current package
(<code>Foo</code>), not to the superclasses of <code>$self</code>.
</para>
<para>
<code>SUPER</code> はカレントパッケージのスーパークラス(<code>Foo</code>) を参照するのであって、
<code>$self</code> のスーパークラスではないことに注意してください。
</para>
</sect1>
<sect1>
<title>USING RELATIONSHIP WITH SDBM</title>
<para>
(SDBM での実装関係)
</para>
<para>
This example demonstrates an interface for the SDBM class.  This creates a
&quot;using&quot; relationship between the SDBM class and the new class Mydbm.
</para>
<para>
この例は SDBM クラスに対するインターフェースを示します。
この例では、SDBM クラスと、新しいクラス Mydbm との間の実装(&quot;using&quot;)関係を
作り出します。
</para>
<verbatim><![CDATA[
package Mydbm;
]]></verbatim>
<verbatim><![CDATA[
require SDBM_File;
require Tie::Hash;
@ISA = qw( Tie::Hash );
]]></verbatim>
<verbatim><![CDATA[
sub TIEHASH {
    my $type = shift;
    my $ref  = SDBM_File->new(@_);
    bless {'dbm' => $ref}, $type;
}
sub FETCH {
    my $self = shift;
    my $ref  = $self->{'dbm'};
    $ref->FETCH(@_);
}
sub STORE {
    my $self = shift;
    if (defined $_[0]){
	my $ref = $self->{'dbm'};
	$ref->STORE(@_);
    } else {
	die "Cannot STORE an undefined key in Mydbm\n";
    }
}
]]></verbatim>
<verbatim><![CDATA[
package main;
use Fcntl qw( O_RDWR O_CREAT );
]]></verbatim>
<verbatim><![CDATA[
tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640;
$foo{'bar'} = 123;
print "foo-bar = $foo{'bar'}\n";
]]></verbatim>
<verbatim><![CDATA[
tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640;
$bar{'Cathy'} = 456;
print "bar-Cathy = $bar{'Cathy'}\n";
]]></verbatim>
</sect1>
<sect1>
<title>THINKING OF CODE REUSE</title>
<para>
(コードの再利用を考える)
</para>
<para>
One strength of Object-Oriented languages is the ease with which old code
can use new code.  The following examples will demonstrate first how one can
hinder code reuse and then how one can promote code reuse.
</para>
<para>
オブジェクト指向言語の一つの強みとは、古いコードと新しいコードを
混ぜて使うのが簡単だと言うことです。
以下の例では、まず最初にどのようにコードの再利用を妨害するかということを、
続いてどのようにコードの再利用を促進するかということを示します。
</para>
<para>
This first example illustrates a class which uses a fully-qualified method
call to access the &quot;private&quot; method BAZ().  The second example will show
that it is impossible to override the BAZ() method.
</para>
<para>
最初の例では、「プライベート」なメソッド BAZ() にアクセスするために
完全修飾メソッド呼び出しを使っているクラスをお見せします。
二番目の例ではオーバーライドすることのできない BAZ() メソッドを
お見せします。
</para>
<verbatim><![CDATA[
package FOO;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	bless {}, $type;
}
sub bar {
	my $self = shift;
	$self->FOO::private::BAZ;
}
]]></verbatim>
<verbatim><![CDATA[
package FOO::private;
]]></verbatim>
<verbatim><![CDATA[
sub BAZ {
	print "in BAZ\n";
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = FOO->new;
$a->bar;
]]></verbatim>
<para>
Now we try to override the BAZ() method.  We would like FOO::bar() to call
GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
FOO::private::BAZ().
</para>
<para>
今度はメソッド BAZ() をオーバーライドしてみましょう。
FOO::bar() で GOOP::BAZ() を呼び出したいのですが、これは FOO::bar() が陽に
FOO::private::BAZ() を呼び出しているのでできません。
</para>
<verbatim><![CDATA[
package FOO;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	bless {}, $type;
}
sub bar {
	my $self = shift;
	$self->FOO::private::BAZ;
}
]]></verbatim>
<verbatim><![CDATA[
package FOO::private;
]]></verbatim>
<verbatim><![CDATA[
sub BAZ {
	print "in BAZ\n";
}
]]></verbatim>
<verbatim><![CDATA[
package GOOP;
@ISA = qw( FOO );
sub new {
	my $type = shift;
	bless {}, $type;
}
]]></verbatim>
<verbatim><![CDATA[
sub BAZ {
	print "in GOOP::BAZ\n";
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = GOOP->new;
$a->bar;
]]></verbatim>
<para>
To create reusable code we must modify class FOO, flattening class
FOO::private.  The next example shows a reusable class FOO which allows the
method GOOP::BAZ() to be used in place of FOO::BAZ().
</para>
<para>
再利用可能なコードを作成するには、クラス FOO を修正しなければならず、
クラス FOO:private を平らにします。
次の例では、FOO::BAZ() を使っている場所でメソッド GOOP:BAZ を
置くことのできるクラス FOO をお見せします。
</para>
<verbatim><![CDATA[
package FOO;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	bless {}, $type;
}
sub bar {
	my $self = shift;
	$self->BAZ;
}
]]></verbatim>
<verbatim><![CDATA[
sub BAZ {
	print "in BAZ\n";
}
]]></verbatim>
<verbatim><![CDATA[
package GOOP;
@ISA = qw( FOO );
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	bless {}, $type;
}
sub BAZ {
	print "in GOOP::BAZ\n";
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = GOOP->new;
$a->bar;
]]></verbatim>
</sect1>
<sect1>
<title>CLASS CONTEXT AND THE OBJECT</title>
<para>
(クラスのコンテキストとオブジェクト)
</para>
<para>
Use the object to solve package and class context problems.  Everything a
method needs should be available via the object or should be passed as a
parameter to the method.
</para>
<para>
パッケージとクラスコンテキストの問題を解決するためにオブジェクトを
使いましょう。
メソッドが必要とするすべてはオブジェクトを通して使用可能であるべきであり、
メソッドに対するパラメータとして渡されるべきなのです。
</para>
<para>
A class will sometimes have static or global data to be used by the
methods.  A subclass may want to override that data and replace it with new
data.  When this happens the superclass may not know how to find the new
copy of the data.
</para>
<para>
あるクラスが、ときとしてメソッドから使うためにスタティックなデータや
グローバルなデータを持つことがあるでしょう。
サブクラスでそのようなデータをオーバーライドして、それを新しいデータに
置き換えたいことがあるかもしれません。
このような事態になったとき、スーパークラスは新しいデータのコピーを
どうやって見つけるのかを知ることができません。
</para>
<para>
This problem can be solved by using the object to define the context of the
method.  Let the method look in the object for a reference to the data.  The
alternative is to force the method to go hunting for the data (&quot;Is it in my
class, or in a subclass?  Which subclass?&quot;), and this can be inconvenient
and will lead to hackery.  It is better just to let the object tell the
method where that data is located.
</para>
<para>
この問題は、メソッドのコンテキストを定義するためのオブジェクトを
使うことによって解決できます。
オブジェクト中にあるメソッドにデータのリファレンスを見せましょう。
もう一つの手段はメソッドにデータを探しまわさせる
(「自分のクラスにあるの? それともサブクラス? どのサブクラス?」)ことですが、
これは不便ですし、ハッカー的にすぎます。
オブジェクトに、データがどこに位置するかを知らせるメソッドを持つように
させるのが良いのです。
</para>
<verbatim><![CDATA[
package Bar;
]]></verbatim>
<verbatim><![CDATA[
%fizzle = ( 'Password' => 'XYZZY' );
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = {};
	$self->{'fizzle'} = \%fizzle;
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
sub enter {
	my $self = shift;
]]></verbatim>
<verbatim><![CDATA[
# Don't try to guess if we should use %Bar::fizzle
# or %Foo::fizzle.  The object already knows which
# we should use, so just ask it.
#
my $fizzle = $self->{'fizzle'};
]]></verbatim>
<verbatim><![CDATA[
# %Bar::fizzle と  %Foo::fizzle のいずれを使うべき
# なのかを推測しようとしないこと。オブジェクトは既
# に答を知っています。だから、それを訊ねるだけです。
#
my $fizzle = $self->{'fizzle'};
]]></verbatim>
<verbatim><![CDATA[
print "The word is ", $fizzle->{'Password'}, "\n";
	}
]]></verbatim>
<verbatim><![CDATA[
package Foo;
@ISA = qw( Bar );
]]></verbatim>
<verbatim><![CDATA[
%fizzle = ( 'Password' => 'Rumple' );
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = Bar->new;
	$self->{'fizzle'} = \%fizzle;
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = Bar->new;
$b = Foo->new;
$a->enter;
$b->enter;
]]></verbatim>
</sect1>
<sect1>
<title>INHERITING A CONSTRUCTOR</title>
<para>
(コンストラクタの継承)
</para>
<para>
An inheritable constructor should use the second form of bless() which allows
blessing directly into a specified class.  Notice in this example that the
object will be a BAR not a FOO, even though the constructor is in class FOO.
</para>
<para>
継承可能なコンストラクタは、特定のクラスに直接 bless することのできる
bless() の第二形式を使うべきです。
以下の例において、クラス FOO のコンストラクタを通じて構成された
オブジェクトであってもそれは FOO ではなく、BAR として存在するように
なることに注意してください。
</para>
<verbatim><![CDATA[
package FOO;
]]></verbatim>
<verbatim><![CDATA[
sub new {
	my $type = shift;
	my $self = {};
	bless $self, $type;
}
]]></verbatim>
<verbatim><![CDATA[
sub baz {
	print "in FOO::baz()\n";
}
]]></verbatim>
<verbatim><![CDATA[
package BAR;
@ISA = qw(FOO);
]]></verbatim>
<verbatim><![CDATA[
sub baz {
	print "in BAR::baz()\n";
}
]]></verbatim>
<verbatim><![CDATA[
package main;
]]></verbatim>
<verbatim><![CDATA[
$a = BAR->new;
$a->baz;
]]></verbatim>
</sect1>
<sect1>
<title>DELEGATION</title>
<para>
(委任)
</para>
<para>
Some classes, such as SDBM_File, cannot be effectively subclassed because
they create foreign objects.  Such a class can be extended with some sort of
aggregation technique such as the &quot;using&quot; relationship mentioned earlier or
by delegation.
</para>
<para>
SDBM_File のようなクラスは外部オブジェクトを生成するので、効率良く
サブクラス化することができません。
そのようなクラスは実装(&quot;using&quot;)関係のような先に言及したものや、
委任のような集成テクニックを用いることで拡張することができます。
</para>
<para>
The following example demonstrates delegation using an AUTOLOAD() function to
perform message-forwarding.  This will allow the Mydbm object to behave
exactly like an SDBM_File object.  The Mydbm class could now extend the
behavior by adding custom FETCH() and STORE() methods, if this is desired.
</para>
<para>
以下の例では、メッセージフォワーディングを実現するために
AUTOLOAD 関数を使っている委任をお見せします。
これは、Mydbm オブジェクトが SDBM_File オブジェクトと全く同じように
振る舞うようにさせるものです。
カスタマイズした FETCH() メソッドや STORE() メソッドを追加することによって、
今や Mydbm クラスを拡張することもできるのです。
</para>
<verbatim><![CDATA[
package Mydbm;
]]></verbatim>
<verbatim><![CDATA[
require SDBM_File;
require Tie::Hash;
@ISA = qw(Tie::Hash);
]]></verbatim>
<verbatim><![CDATA[
sub TIEHASH {
	my $type = shift;
	my $ref = SDBM_File->new(@_);
	bless {'delegate' => $ref};
}
]]></verbatim>
<verbatim><![CDATA[
sub AUTOLOAD {
	my $self = shift;
]]></verbatim>
<verbatim><![CDATA[
# The Perl interpreter places the name of the
# message in a variable called $AUTOLOAD.
]]></verbatim>
<verbatim><![CDATA[
# Perl インタープリターはメッセージの名前を
# $AUTOLOAD と呼ばれる変数に置いています
]]></verbatim>
<verbatim><![CDATA[
# DESTROY messages should never be propagated.
return if $AUTOLOAD =~ /::DESTROY$/;
]]></verbatim>
<verbatim><![CDATA[
# DESTROY メッセージは伝播させるべきでない
return if $AUTOLOAD =~ /::DESTROY$/;
]]></verbatim>
<verbatim><![CDATA[
# Remove the package name.
$AUTOLOAD =~ s/^Mydbm:://;
]]></verbatim>
<verbatim><![CDATA[
# パッケージ名を取り除く
$AUTOLOAD =~ s/^Mydbm:://;
]]></verbatim>
<verbatim><![CDATA[
# Pass the message to the delegate.
$self->{'delegate'}->$AUTOLOAD(@_);
	}
]]></verbatim>
<verbatim><![CDATA[
# delegate にメッセージを渡す
$self->{'delegate'}->$AUTOLOAD(@_);
	}
]]></verbatim>
<verbatim><![CDATA[
package main;
use Fcntl qw( O_RDWR O_CREAT );
]]></verbatim>
<verbatim><![CDATA[
tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640;
$foo{'bar'} = 123;
print "foo-bar = $foo{'bar'}\n";
]]></verbatim>
</sect1>
<sect1>
<title>SEE ALSO</title>
<para>
<link xref='perlboot'>perlboot</link>, <link xref='perltoot'>perltoot</link>, <link xref='perltooc'>perltooc</link>.
</para>
<para>
Created: KIMURA Koichi
Updated: Kentaro Shirakata &lt;argrath@ub32.org&gt;
</para>
</sect1>
</pod>
