use overload の特殊シンボル
overload - Perl の演算子の多重定義を行うパッケージ
package SomeThing;
use overload
'+' => \&myadd,
'-' => \&mysub;
# etc
...
package main;
$a = new SomeThing 57;
$b=5+$a;
...
if (overload::Overloaded $b) {...}
...
$strval = overload::StrVal $b;
コンパイル指示子
package Number;
use overload
"+" => \&add,
"*=" => "muas";
では、加法の関数 Number::add() と「クラス」Number (あるいは、
基底クラスの 1 つ) の中の乗法の代入形式 *= のメソッド muas()
を宣言しています。
この指示子の引数は (key, value) のペアです。
この value としては、&{ ... } の中で使用できるものが
すべてを指定できますから、サブルーチン名、
サブルーチンへのリファレンス、無名のサブルーチンといったものが
すべて使えます。
文字列として指定された値はサブルーチンではなく、
メソッドとして解釈されることに注意してください。
key として有効な値は以下に述べます。
$a+$b を実行するときに、$a がパッケージ Number 内に
bless されたオブジェクトへのリファレンスである場合か、
$a がそのようなマスマジカルな加法を用意しているパッケージの
オブジェクトでなくても、$b が Number へのリファレンスである場合に、
サブルーチン add が呼び出されます。
これは、$a+=7 とか $a++ といった、シチュエーションでも呼ばれます。
MAGIC AUTOGENERATION の節を参照してください。
(「マスマジカル」という言葉は、多重定義された
マスマティカル演算子によって起動されるメソッドを指しています。)
Since overloading respects inheritance via the @ISA hierarchy, the
above declaration would also trigger overloading of + and *= in
all the packages which inherit from Number.
use overload ... 指示子の value の関数は、
3 つ (唯一特別な場合があって、
その時は 4 つ (Last Resortの節を参照) ) の引数で呼び出されます。
対応する演算子が、二項演算子であれば、最初の 2 つの引数は、
その演算子の 2 つの引数です。
しかしながら、通常のオブジェクトメソッドの呼び出し法の規則によって、
最初の引数は、常にそのパッケージのオブジェクトでなければなりませんので、
7+$a のような場合には、引数の順序の入れ替えが行なわれます。
これは、加法のメソッドを実装する時には、
おそらく問題にはならないものですが、減法のメソッドにとっては、
引数を入替えるか否かは、非常に重大な問題です。
メソッド側では、この引数の入れ替えについての情報を 3 つめの引数を
調べることで、確かめることができます。
この引数は、3 種類の値をとります:
引数の順序は、現在の演算子でのものと同じである。
引数は、逆になっている。
undef現在の演算子は、($a+=7 のような) 代入形式のものであるが、
普通の関数が、代わりに呼ばれる。
この付加的な情報は、何らかの最適化を行なうときに、
使用することができる。
Calling Conventions for Mutators と比較してください。
単項演算子は、2 番目の引数が undef の二項演算子であると
考えられます。
つまり、{"++"} を多重定義する関数は、
$a++ が実行されるときに、($a, undef, '') という引数で呼び出されます。
Two types of mutators have different calling conventions:
++ and --
sub incr { my $n = $ {$_[0]}; ++$n; $_[0] = bless \$n}
is an appropriate implementation of overloaded ++. Note that
sub incr { ++$ {$_[0]} ; shift }
is OK if used with preincrement and with postincrement. (In the case of postincrement a copying will be performed, see Copy Constructor.)
x= and other assignment versionsThis allows for the same method to be used as overloaded += and
+. Note that this is allowed, but not recommended, since by the
semantic of Fallback Perl will call the method for + anyway,
if += is not overloaded.
Warning. Due to the presense of assignment versions of operations,
routines which may be called in assignment context may create
self-referential structures. Currently Perl will not free self-referential
structures until cycles are explicitly broken. You may get problems
when traversing your structures too.
Say,
use overload '+' => sub { bless [ \$_[0], \$_[1] ] };
is asking for trouble, since for code $obj += $foo the subroutine
is called as $obj = add($obj, $foo, undef), or $obj = [\$obj,
\$foo]. If using such a subroutine is an important optimization, one
can overload += explicitly by a non-``optimized'' version, or switch
to non-optimized version if not defined $_[2] (see
Calling Conventions for Binary Operations).
Even if no explicit assignment-variants of operators are present in
the script, they may be generated by the optimizer. Say, ",$obj," or
',' . $obj . ',' may be both optimized to
my $tmp = ',' . $obj; $tmp .= ',';
以下のシンボルが use overload 指示子で指定できます:
"+", "+=", "-", "-=", "*", "*=", "/", "/=", "%", "%=",
"**", "**=", "<<", "<<=", ">>", ">>=", "x", "x=", ".", ".=",
これらの演算子について、代入形式のものが存在しないとき
には、代わりに非代入形式のものが呼ばれます。
演算子
"+", "-", "+=", "-=" に対するメソッドは、インクリメント
演算子やデクリメント演算子を自動生成するために呼ばれる
ことがあります。
演算子 "-" は、単項のマイナスや abs の
メソッドがないときに自動生成するために使われます。
これらの置換に関する詳細については MAGIC AUTOGENERATION, Calling Conventions for Mutators, Calling Conventions for Binary Operations を参照して下さい。
"<", "<=", ">", ">=", "==", "!=", "<=>",
"lt", "le", "gt", "ge", "eq", "ne", "cmp",
ある演算子が無い場合にも、対応する「スペースシップ」形式が使えるならば、
代わりに使うことができます。
配列のソートのときには、use overload のもとの cmp を使って値を
比較します。
"&", "^", "|", "neg", "!", "~",
``neg'' は、単項のマイナスを表わします。
neg のメソッドが指定されていないときには、
引き算のメソッドを使って、自動生成されます。
``!'' のメソッドが指定されていないときには、
``bool'', ``\"\"'', ``0+'' のいずれかのメソッドを使って
自動生成されます。
"++", "--",
未定義であれば、足し算と引き算のメソッドが代わりに使われます。 これらの演算子は、プリフィクスとしても、ポストフィクスとしても使われます。
"atan2", "cos", "sin", "exp", "abs", "log", "sqrt",
abs がないときには、``<'' か ``<=<>'' のメソッドを、
単項のマイナスか引き算のメソッドと組み合わせて、
自動生成されます。
"bool", "\"\"", "0+",
これらの中で多重定義していないものがあっても、残りが一つで
も定義してあれば、それを代わりに使うことができます。
bool は、(while のような) フロー制御演算子や、
三項演算子 ``?:'' で使われます。
これらの関数は、任意の Perl値を返すことができます。
この値に対応する演算子も多重定義されている場合には、
その演算子がその時の値を使って、再度呼び出されることになります。
As a special case if the overload returns the object itself then it will
be used directly. An overloaded conversion returning the object is
probably a bug, because you're likely to get something that looks like
YourPackage=HASH(0x8172b34).
"<>"
If not overloaded, the argument will be converted to a filehandle or
glob (which may require a stringification). The same overloading
happens both for the read-filehandle syntax <$var> and
globbing syntax <${var}>.
'${}', '@{}', '%{}', '&{}', '*{}'.
If not overloaded, the argument will be dereferenced as is, thus should be of correct type. These functions should return a reference of correct type, or another object with overloaded dereferencing.
As a special case if the overload returns the object itself then it will be used directly (provided it is the correct type).
The dereference operators must be specified explicitly they will not be passed to ``nomethod''.
"nomethod", "fallback", "=",
SPECIAL SYMBOLS FOR use overload を参照してください。
存在しないメソッドの自動生成についての説明は Fallback を参照して下さい。
A computer-readable form of the above table is available in the hash %overload::ops, with values being space-separated lists of names:
with_assign => '+ - * / % ** << >> x .',
assign => '+= -= *= /= %= **= <<= >>= x= .=',
num_comparison => '< <= > >= == !=',
'3way_comparison'=> '<=> cmp',
str_comparison => 'lt le gt ge eq ne',
binary => '& | ^',
unary => 'neg ! ~',
mutators => '++ --',
func => 'atan2 cos sin exp abs log sqrt',
conversion => 'bool "" 0+',
iterators => '<>',
dereferencing => '${} @{} %{} &{} *{}',
special => 'nomethod fallback ='
継承はオーバーロードに二つの方法で関わります。
use overload directivevalue in
use overload key => value;
is a string, it is interpreted as a method name.
If A inherits from B and C (in this order), B overloads
+ with \&D::plus_sub, and C overloads + by "plus_meth",
then the subroutine D::plus_sub will be called to implement
operation + for an object in package A.
Note that since the value of the fallback key is not a subroutine,
its inheritance is not governed by the above rules. In the current
implementation, the value of fallback in the first overloaded
ancestor is used, but this is accidental and subject to change.
use overload の特殊シンボルここまでに説明してきたものの他に、3 つの key が Perl に認識されます。
"nomethod" は、4 つのパラメータを持つ関数へのリファレンスが引き続きます。
これが定義されていれば、多重定義の仕組みで、
何らかの演算子に対するメソッドを見つけることができなかったときに、
呼び出されます。
この関数の最初の 3 つの引数は、本来、
呼ばれるはずだったメソッドに対する引数と一致し、4 番目の引数は、
見つからなかったメソッドに対応するシンボルとなります。
いくつかのメソッドが試されている場合には、最後のものが使われます。
たとえば、1-$a であれば、
"nomethod" => "nomethodMethod" の組が use overload 指示子で
指定されていれば:
&nomethodMethod($a,1,1,"-")
と同様です。
"nomethod" の機構は デリファレンス演算子 ( ${} @{} %{} &{} *{} ) では
使用されません。
何らかの演算子が見つからず、"nomethod" に結び付けられた関数もない
場合には、("fallback" が use overload 指示子のキーとして
指定されていない限り) die() による例外が発生します。
"fallback" は、特定の演算子に対するメソッドが見つからない場合の
動作を規定します。
"fallback" の value によって、3 つの場合があります:
undefPerl は、代替のメソッドを使うことを試みます
(MAGIC AUTOGENERATION の節を参照してください)。
それもダメならば、"nomethod" を呼び出そうとします。
これも無い場合には、例外が発生することになります。
undef の場合と同じですが、例外を発生させません。
この場合、黙って、もし use overload がなかったときに、
行なってであろう動作に戻されることになります。
マジック自動生成は行ないません。
Perl は、まず "nomethod" の実行を試みて、
これがなければ、例外を発生させます。
注意: @ISA 経由の "fallback" 継承はまだ行われません。
Inheritance and overloading を参照して下さい。
"=" の値は、3 引数の関数へのリファレンスです。
つまり、use overload の他の値と似ているように見えます。
しかし、これは Perl の代入演算子を多重定義しません。
これは「ラクダの毛(Camel hair)」に対抗しています。
この演算は、以下のような、他のリファレンスとオブジェクトを共有する リファレンスに対して、ミューテーターを使うときに呼び出されます。
$a=$b;
++$a;
これを、$a を変更し、$b を変更しないようにするために、$$a のコピーを作り、
この新しいオブジェクトへのリファレンスが $a に代入されます。
この操作は、++$a の実行中に (すなわち、その前に
$$a が $$b に一致します)、行われます。
これは++ が '++' か '+=' (か nomethod) のメソッドを通じて
表現されているときにだけ行なわれます。
この演算子が、非ミューテーター "+" を使って記述されている場合、
$a=$b;
$a=$a+1;
$a は $$a の新しいコピーのリファレンスではありません。
上記のコードが実行されたときに $$a は左辺値としては現れていないからです。
コピーコンストラクタが、いくつかのミューテーターの実行中に必要となって、
'=' が指定されていないときには、そのオブジェクトが
単なるスカラであれば、文字列コピーとして自動生成されます。
The actually executed code for
$a=$b;
Something else which does not modify $a or $b....
++$a;
may be
$a=$b;
Something else which does not modify $a or $b....
$a = $a->clone(undef,"");
$a->incr(undef,"");
if $b was mathemagical, and '++' was overloaded with \&incr,
'=' was overloaded with \&clone.
Same behaviour is triggered by $b = $a++, which is consider a synonym for
$b = $a; ++$a.
演算子に対するメソッドが見つからず、"fallback" が
「真」か「未定義」であれば、Perl は、定義されている演算子を
もとに、見つからなかった演算子の代わりのメソッドを自動生成しようと試みます。
以下の演算子に対して、自動生成代替メソッドが行なえます:
"+=" メソッドが定義されていないとき、
$a+=$b は、"+" メソッドを使うことができます。
文字列、数値、ブール値変換は、すべてが定義されてはいないとき、 互いに別のもので計算されます。
演算 ++$a は、$a+=1 か $a+1 で、演算 $a-- は、
$a-=1 か $a-1 で表現することができます。
abs($a)abs($a) は、$a<0 と -$a (または 0-$a) で表現できます。
単項のマイナスは、引き算を使って表現できます。
! と not はブール値変換、文字列変換、数値変換を使って
表現できます。
連結は、文字列変換を使って表現できます。
比較演算は、それぞれに対応する「スペースシップ」演算
(<=<> か cmp) を用いて表現することができます:
<, >, <=, >=, ==, != in terms of <=>
lt, gt, le, ge, eq, ne in terms of cmp
<> in terms of builtin operations
${} @{} %{} &{} *{} in terms of builtin operations
コピー演算は被参照した値が、リファレンスではないスカラであれば、 その値への代入という形で表現できます。
比較演算子に対する制限は、たとえば、`cmp' が bless された
リファレンスを返さなければならないとしても、自動生成された関数
`lt' は、`cmp' の結果の数値に基づく標準の論理値だけを
作り出します。
特に、この場合には、(ときには別の変換で表わされた)
数値変換が使えないといけません。
同様に、.= 演算子や x= 演算子も、文字列変換による代替が起これば、
マスマジカルな性質がなくなります。
マスマジカルなオブジェクトを chop() すると、文字列になり、
マスマジカルな性質はなくなります。 同じことは、他の演算でも
起こります。
Since all use directives are executed at compile-time, the only way to
change overloading during run-time is to
eval 'use overload "+" => \&addmethod';
You can also use
eval 'no overload "+", "--", "<="';
though the use of these constructs during run-time is questionable.
Package overload.pm provides the following public functions:
arg as in absence of stringify overloading.
arg is subject to overloading of some operations.
undef or a reference to the method that implements op.
アプリケーションによっては Perl パーザが定数をいじりすぎる場合があります。 この処理を overload::constant() 関数と overload::remove_constant() 関数を 使ってフックできます。
これらの関数は引数としてハッシュを取ります。 ハッシュのキーとして認識されるのは以下のものです。
q-quoted strings, constant pieces of qq- and qx-quoted
strings and here-documents,
The corresponding values are references to functions which take three arguments:
the first one is the initial string form of the constant, the second one
is how Perl interprets this constant, the third one is how the constant is used.
Note that the initial string form does not
contain string delimiters, and has backslashes in backslash-delimiter
combinations stripped (thus the value of delimiter is not relevant for
processing of this string). The return value of this function is how this
constant is going to be interpreted by Perl. The third argument is undefined
unless for overloaded q- and qr- constants, it is q in single-quote
context (comes from strings, regular expressions, and single-quote HERE
documents), it is tr for arguments of tr/y operators,
it is s for right-hand side of s-operator, and it is qq otherwise.
Since an expression "ab$cd,," is just a shortcut for 'ab' . $cd . ',,',
it is expected that overloaded constant strings are equipped with reasonable
overloaded catenation operator, otherwise absurd results will result.
Similarly, negative numbers are considered as negations of positive constants.
Note that it is probably meaningless to call the functions overload::constant()
and overload::remove_constant() from anywhere but import() and unimport() methods.
From these methods they may be called as
sub import {
shift;
return unless @_;
die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant';
overload::constant integer => sub {Math::BigInt->new(shift)};
}
BUGS Currently overloaded-ness of constants does not propagate
into eval '...'.
以下はすぐに変更される可能性があります。
すべての演算のためのメソッドのテーブルは、該当パッケージの
シンボルテーブルに対するマジックとしてキャッシュされます。
このキャッシュは use overload, no overload, 新しい関数定義、
@ISA の変更のいずれかの処理の間に無効化されます。
しかし、この無効化はパッケージに対する次の bless までは
実行されずに残されます。
つまり、多重定義構造を動的に変更したいならば、テーブルを
更新するために、(意味の無い) bless を行なう必要があります。
(すべての SV 風のものは、マジックキューを持っており、マジックが キューのエントリになっています。 これによって、1 つの変数が、同時に複数のマジックの形式に 関ることができるのです。 たとえば、環境変数は普段、%ENV マジックと「汚染」マジックの 2 つの形式を一度に持っています。 しかし、多重定義を実装しているマジックは隠してあるものに 適用され、これはめったに直接使うことはないため、 Perl の速度を低下させないはずです。)
オブジェクトが多重定義を使うパッケージに属するならば、 そのオブジェクトには、特別なフラグが用意されます。 つまり、多重定義されていない算術演算を行なうときの、 スピードに対する影響は、このフラグのチェックのみです。
実際、use overload が存在しなければ、多重定義可能な演算に
対するオーバヘッドはほとんど無く、ほとんどのプログラムで、
認識できるようなパフォーマスの低下はないはずです。
あるパッケージで多重定義が使われても、
対象の引数が多重定義を使ったパッケージに属していない場合には、
オーバヘッドの最小限にする最大限の努力が為されました。
疑わしいときには、use overload がある場合と無い場合で、
スピードのテストをしてください。 これまでのところ、Perl が
最適化を指定してコンパイル場合には、顕著なスピードの低下の報告は
あがっていません。
多重定義が使われないときには、データの大きさには影響しません。
あるパッケージで多重定義を使うときの唯一のサイズペナルティは、
全てのパッケージが次のパッケージへの bless 時に
マジックを求めることです。
このマジックは多重定義を使わないパッケージの場合は 3 ワード長で、
多重定義を使うパッケージの場合はキャッシュテーブルを運びます。
$a=$b のようなコピーは、表層的なものです。
しかし、$a++ のように、$b (または、$a) が参照するオブジェクトへの
代入を意味する演算の前に、1 層深度のコピーが行なわれます。
この動作は、
自分でコピーコンストラクタを定義することによって変更することが
できます (Copy Constructorの項を参照してください)。
明示的にサポートされていないメソッドに対する引数は、 定数であることが期待されます (が、強制はされません)。
One may wonder why the semantic of overloaded = is so counter intuitive.
If it looks counter intuitive to you, you are subject to a metaphor
clash.
Here is a Perl object metaphor:
object is a reference to blessed data
and an arithmetic metaphor:
object is a thing by itself.
The main problem of overloading = is the fact that these metaphors
imply different actions on the assignment $a = $b if $a and $b are
objects. Perl-think implies that $a becomes a reference to whatever
$b was referencing. Arithmetic-think implies that the value of ``object''
$a is changed to become the value of the object $b, preserving the fact
that $a and $b are separate entities.
The difference is not relevant in the absence of mutators. After
a Perl-way assignment an operation which mutates the data referenced by $a
would change the data referenced by $b too. Effectively, after
$a = $b values of $a and $b become indistinguishable.
On the other hand, anyone who has used algebraic notation knows the expressive power of the arithmetic metaphor. Overloading works hard to enable this metaphor while preserving the Perlian way as far as possible. Since it is not not possible to freely mix two contradicting metaphors, overloading allows the arithmetic way to write things as far as all the mutators are called via overloaded access only. The way it is done is described in Copy Constructor.
If some mutator methods are directly applied to the overloaded values, one may need to explicitly unlink other values which references the same value:
$a = new Data 23;
...
$b = $a; # $b is "linked" to $a
...
$a = $a->clone; # Unlink $b from $a
$a->increment_by(4);
Note that overloaded access makes this transparent:
$a = new Data 23;
$b = $a; # $b is "linked" to $a
$a += 4; # would unlink $b automagically
However, it would not make
$a = new Data 23;
$a = 4; # Now $a is a plain 4, not 'Data'
preserve ``objectness'' of $a. But Perl has a way to make assignments
to an object do whatever you want. It is just not the overload, but
tie()ing interface (see tie in the perlfunc manpage). Adding a FETCH() method
which returns the object itself, and STORE() method which changes the
value of the object, one can reproduce the arithmetic metaphor in its
completeness, at least for variables which were tie()d from the start.
(Note that a workaround for a bug may be needed, see BUGS.)
Please add examples to what follows!
Put this in two_face.pm in your Perl library directory:
package two_face; # Scalars with separate string and
# numeric values.
sub new { my $p = shift; bless [@_], $p }
use overload '""' => \&str, '0+' => \&num, fallback => 1;
sub num {shift->[1]}
sub str {shift->[0]}
Use it as follows:
require two_face;
my $seven = new two_face ("vii", 7);
printf "seven=$seven, seven=%d, eight=%d\n", $seven, $seven+1;
print "seven contains `i'\n" if $seven =~ /i/;
(The second line creates a scalar which has both a string value, and a numeric value.) This prints:
seven=vii, seven=7, eight=8 seven contains `i'
Suppose you want to create an object which is accessible as both an array reference and a hash reference, similar to the pseudo-hash builtin Perl type. Let's make it better than a pseudo-hash by allowing index 0 to be treated as a normal element.
package two_refs;
use overload '%{}' => \&gethash, '@{}' => sub { $ {shift()} };
sub new {
my $p = shift;
bless \ [@_], $p;
}
sub gethash {
my %h;
my $self = shift;
tie %h, ref $self, $self;
\%h;
}
sub TIEHASH { my $p = shift; bless \ shift, $p }
my %fields;
my $i = 0;
$fields{$_} = $i++ foreach qw{zero one two three};
sub STORE {
my $self = ${shift()};
my $key = $fields{shift()};
defined $key or die "Out of band access";
$$self->[$key] = shift;
}
sub FETCH {
my $self = ${shift()};
my $key = $fields{shift()};
defined $key or die "Out of band access";
$$self->[$key];
}
Now one can access an object using both the array and hash syntax:
my $bar = new two_refs 3,4,5,6;
$bar->[2] = 11;
$bar->{two} == 11 or die 'bad hash fetch';
Note several important features of this example. First of all, the
actual type of $bar is a scalar reference, and we do not overload
the scalar dereference. Thus we can get the actual non-overloaded
contents of $bar by just using $$bar (what we do in functions which
overload dereference). Similarly, the object returned by the
TIEHASH() method is a scalar reference.
Second, we create a new tied hash each time the hash syntax is used. This allows us not to worry about a possibility of a reference loop, would would lead to a memory leak.
Both these problems can be cured. Say, if we want to overload hash dereference on a reference to an object which is implemented as a hash itself, the only problem one has to circumvent is how to access this actual hash (as opposed to the virtual hash exhibited by the overloaded dereference operator). Here is one possible fetching routine:
sub access_hash {
my ($self, $key) = (shift, shift);
my $class = ref $self;
bless $self, 'overload::dummy'; # Disable overloading of %{}
my $out = $self->{$key};
bless $self, $class; # Restore overloading
$out;
}
To remove creation of the tied hash on each access, one may an extra level of indirection which allows a non-circular structure of references:
package two_refs1;
use overload '%{}' => sub { ${shift()}->[1] },
'@{}' => sub { ${shift()}->[0] };
sub new {
my $p = shift;
my $a = [@_];
my %h;
tie %h, $p, $a;
bless \ [$a, \%h], $p;
}
sub gethash {
my %h;
my $self = shift;
tie %h, ref $self, $self;
\%h;
}
sub TIEHASH { my $p = shift; bless \ shift, $p }
my %fields;
my $i = 0;
$fields{$_} = $i++ foreach qw{zero one two three};
sub STORE {
my $a = ${shift()};
my $key = $fields{shift()};
defined $key or die "Out of band access";
$a->[$key] = shift;
}
sub FETCH {
my $a = ${shift()};
my $key = $fields{shift()};
defined $key or die "Out of band access";
$a->[$key];
}
Now if $baz is overloaded like this, then $baz is a reference to a
reference to the intermediate array, which keeps a reference to an
actual array, and the access hash. The tie()ing object for the access
hash is a reference to a reference to the actual array, so
two_refs1 are
references to a reference to an array, thus references to a scalar.
Thus the accessor expression $$foo->[$ind] involves no
overloaded operations.
この symbolic.pm をあなたの Perl ライブラリディレクトリに入れてください:
package symbolic; # Primitive symbolic calculator use overload nomethod => \&wrap;
sub new { shift; bless ['n', @_] }
sub wrap {
my ($obj, $other, $inv, $meth) = @_;
($obj, $other) = ($other, $obj) if $inv;
bless [$meth, $obj, $other];
}
This module is very unusual as overloaded modules go: it does not
provide any usual overloaded operators, instead it provides the Last Resort operator nomethod. In this example the corresponding
subroutine returns an object which encapsulates operations done over
the objects: new symbolic 3 contains ['n', 3], 2 + new
symbolic 3 contains ['+', 2, ['n', 3]].
Here is an example of the script which ``calculates'' the side of circumscribed octagon using the above package:
require symbolic; my $iter = 1; # 2**($iter+2) = 8 my $side = new symbolic 1; my $cnt = $iter;
while ($cnt--) {
$side = (sqrt(1 + $side**2) - 1)/$side;
}
print "OK\n";
The value of $side is
['/', ['-', ['sqrt', ['+', 1, ['**', ['n', 1], 2]],
undef], 1], ['n', 1]]
Note that while we obtained this value using a nice little script,
there is no simple way to use this value. In fact this value may
be inspected in debugger (see the perldebug manpage), but ony if
bareStringify Option is set, and not via p command.
If one attempts to print this value, then the overloaded operator
"" will be called, which will call nomethod operator. The
result of this operator will be stringified again, but this result is
again of type symbolic, which will lead to an infinite loop.
Add a pretty-printer method to the module symbolic.pm:
sub pretty {
my ($meth, $a, $b) = @{+shift};
$a = 'u' unless defined $a;
$b = 'u' unless defined $b;
$a = $a->pretty if ref $a;
$b = $b->pretty if ref $b;
"[$meth $a $b]";
}
Now one can finish the script by
print "side = ", $side->pretty, "\n";
The method pretty is doing object-to-string conversion, so it
is natural to overload the operator "" using this method. However,
inside such a method it is not necessary to pretty-print the
components $a and $b of an object. In the above subroutine
"[$meth $a $b]" is a catenation of some strings and components $a
and $b. If these components use overloading, the catenation operator
will look for an overloaded operator .; if not present, it will
look for an overloaded operator "". Thus it is enough to use
use overload nomethod => \&wrap, '""' => \&str;
sub str {
my ($meth, $a, $b) = @{+shift};
$a = 'u' unless defined $a;
$b = 'u' unless defined $b;
"[$meth $a $b]";
}
Now one can change the last line of the script to
print "side = $side\n";
which outputs
side = [/ [- [sqrt [+ 1 [** [n 1 u] 2]] u] 1] [n 1 u]]
and one can inspect the value in debugger using all the possible methods.
Something is is still amiss: consider the loop variable $cnt of the
script. It was a number, not an object. We cannot make this value of
type symbolic, since then the loop will not terminate.
Indeed, to terminate the cycle, the $cnt should become false.
However, the operator bool for checking falsity is overloaded (this
time via overloaded ""), and returns a long string, thus any object
of type symbolic is true. To overcome this, we need a way to
compare an object to 0. In fact, it is easier to write a numeric
conversion routine.
Here is the text of symbolic.pm with such a routine added (and slightly modified str()):
package symbolic; # Primitive symbolic calculator
use overload
nomethod => \&wrap, '""' => \&str, '0+' => \#
sub new { shift; bless ['n', @_] }
sub wrap {
my ($obj, $other, $inv, $meth) = @_;
($obj, $other) = ($other, $obj) if $inv;
bless [$meth, $obj, $other];
}
sub str {
my ($meth, $a, $b) = @{+shift};
$a = 'u' unless defined $a;
if (defined $b) {
"[$meth $a $b]";
} else {
"[$meth $a]";
}
}
my %subr = ( n => sub {$_[0]},
sqrt => sub {sqrt $_[0]},
'-' => sub {shift() - shift()},
'+' => sub {shift() + shift()},
'/' => sub {shift() / shift()},
'*' => sub {shift() * shift()},
'**' => sub {shift() ** shift()},
);
sub num {
my ($meth, $a, $b) = @{+shift};
my $subr = $subr{$meth}
or die "Do not know how to ($meth) in symbolic";
$a = $a->num if ref $a eq __PACKAGE__;
$b = $b->num if ref $b eq __PACKAGE__;
$subr->($a,$b);
}
All the work of numeric conversion is done in %subr and num(). Of course, %subr is not complete, it contains only operators used in the example below. Here is the extra-credit question: why do we need an explicit recursion in num()? (Answer is at the end of this section.)
Use this module like this:
require symbolic; my $iter = new symbolic 2; # 16-gon my $side = new symbolic 1; my $cnt = $iter;
while ($cnt) {
$cnt = $cnt - 1; # Mutator `--' not implemented
$side = (sqrt(1 + $side**2) - 1)/$side;
}
printf "%s=%f\n", $side, $side;
printf "pi=%f\n", $side*(2**($iter+2));
It prints (without so many line breaks)
[/ [- [sqrt [+ 1 [** [/ [- [sqrt [+ 1 [** [n 1] 2]]] 1]
[n 1]] 2]]] 1]
[/ [- [sqrt [+ 1 [** [n 1] 2]]] 1] [n 1]]]=0.198912
pi=3.182598
The above module is very primitive. It does not implement
mutator methods (++, -= and so on), does not do deep copying
(not required without mutators!), and implements only those arithmetic
operations which are used in the example.
To implement most arithmetic operations is easy; one should just use the tables of operations, and change the code which fills %subr to
my %subr = ( 'n' => sub {$_[0]} );
foreach my $op (split " ", $overload::ops{with_assign}) {
$subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
}
my @bins = qw(binary 3way_comparison num_comparison str_comparison);
foreach my $op (split " ", "@overload::ops{ @bins }") {
$subr{$op} = eval "sub {shift() $op shift()}";
}
foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
print "defining `$op'\n";
$subr{$op} = eval "sub {$op shift()}";
}
Due to Calling Conventions for Mutators, we do not need anything
special to make += and friends work, except filling += entry of
%subr, and defining a copy constructor (needed since Perl has no
way to know that the implementation of '+=' does not mutate
the argument, compare Copy Constructor).
To implement a copy constructor, add '=' => \&cpy to use overload
line, and code (this code assumes that mutators change things one level
deep only, so recursive copying is not needed):
sub cpy {
my $self = shift;
bless [@$self], ref $self;
}
To make ++ and -- work, we need to implement actual mutators,
either directly, or in nomethod. We continue to do things inside
nomethod, thus add
if ($meth eq '++' or $meth eq '--') {
@$obj = ($meth, (bless [@$obj]), 1); # Avoid circular referencen
return $obj;
}
after the first line of wrap(). This is not a most effective implementation, one may consider
sub inc { $_[0] = bless ['++', shift, 1]; }
instead.
As a final remark, note that one can fill %subr by
my %subr = ( 'n' => sub {$_[0]} );
foreach my $op (split " ", $overload::ops{with_assign}) {
$subr{$op} = $subr{"$op="} = eval "sub {shift() $op shift()}";
}
my @bins = qw(binary 3way_comparison num_comparison str_comparison);
foreach my $op (split " ", "@overload::ops{ @bins }") {
$subr{$op} = eval "sub {shift() $op shift()}";
}
foreach my $op (split " ", "@overload::ops{qw(unary func)}") {
$subr{$op} = eval "sub {$op shift()}";
}
$subr{'++'} = $subr{'+'};
$subr{'--'} = $subr{'-'};
This finishes implementation of a primitive symbolic calculator in 50 lines of Perl code. Since the numeric values of subexpressions are not cached, the calculator is very slow.
Here is the answer for the exercise: In the case of str(), we need no
explicit recursion since the overloaded .-operator will fall back
to an existing overloaded operator "". Overloaded arithmetic
operators do not fall back to numeric conversion if fallback is
not explicitly requested. Thus without an explicit recursion num()
would convert ['+', $a, $b] to $a + $b, which would just rebuild
the argument of num().
If you wonder why defaults for conversion are different for str() and
num(), note how easy it was to write the symbolic calculator. This
simplicity is due to an appropriate choice of defaults. One extra
note: due to the explicit recursion num() is more fragile than sym():
we need to explicitly check for the type of $a and $b. If components
$a and $b happen to be of some related type, this may lead to problems.
One may wonder why we call the above calculator symbolic. The reason is that the actual calculation of the value of expression is postponed until the value is used.
To see it in action, add a method
sub STORE {
my $obj = shift;
$#$obj = 1;
@$obj->[0,1] = ('=', shift);
}
to the package symbolic. After this change one can do
my $a = new symbolic 3; my $b = new symbolic 4; my $c = sqrt($a**2 + $b**2);
and the numeric value of $c becomes 5. However, after calling
$a->STORE(12); $b->STORE(5);
the numeric value of $c becomes 13. There is no doubt now that the module symbolic provides a symbolic calculator indeed.
To hide the rough edges under the hood, provide a tie()d interface to the
package symbolic (compare with Metaphor clash). Add methods
sub TIESCALAR { my $pack = shift; $pack->new(@_) }
sub FETCH { shift }
sub nop { } # Around a bug
(the bug is described in BUGS). One can use this new interface as
tie $a, 'symbolic', 3; tie $b, 'symbolic', 4; $a->nop; $b->nop; # Around a bug
my $c = sqrt($a**2 + $b**2);
Now numeric value of $c is 5. After $a = 12; $b = 5 the numeric value
of $c becomes 13. To insulate the user of the module add a method
sub vars { my $p = shift; tie($_, $p), $_->nop foreach @_; }
Now
my ($a, $b); symbolic->vars($a, $b); my $c = sqrt($a**2 + $b**2);
$a = 3; $b = 4; printf "c5 %s=%f\n", $c, $c;
$a = 12; $b = 5; printf "c13 %s=%f\n", $c, $c;
shows that the numeric value of $c follows changes to the values of $a and $b.
Ilya Zakharevich <ilya@math.mps.ohio-state.edu>.
Perl を -Do スイッチか同等のものを使って起動すると、 多重定義が、診断メッセージを誘発します。
Using the m command of Perl debugger (see the perldebug manpage) one can
deduce which operations are overloaded (and which ancestor triggers
this overloading). Say, if eq is overloaded, then the method (eq
is shown by debugger. The method () corresponds to the fallback
key (in fact a presence of this method shows that this package has
overloading enabled, and it is what is used by the Overloaded
function of module overload).
The module might issue the following warnings:
多重定義に使用されるため、Perl では、ハッシュ %OVERLOAD は、 パッケージごとに特別な意味を持つことになります。 シンボルテーブルはごみのように見える名前で埋められます。
For the purpose of inheritance every overloaded package behaves as if
fallback is present (possibly undefined). This may create
interesting effects if some package is not overloaded, but inherits
from two overloaded packages.
Relation between overloading and tie()ing is broken. Overloading is triggered or not basing on the previous class of tie()d value.
This happens because the presence of overloading is checked too early, before any tie()d access is attempted. If the FETCH()ed class of the tie()d value does not change, a simple workaround is to access the value immediately after tie()ing, so that after this call the previous class coincides with the current one.
Needed: a way to fix this without a speed penalty.
Barewords are not covered by overloaded string constants.
このドキュメントは混乱しています。あちこちに 誤解しやすい文章があります。完全な書き直しが必要です。