📄 overload.pm
字号:
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, theI<actual> type of $bar is a scalar reference, and we do not overloadthe scalar dereference. Thus we can get the I<actual> non-overloadedcontents of $bar by just using C<$$bar> (what we do in functions whichoverload dereference). Similarly, the object returned by theTIEHASH() 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 hashdereference on a reference to an object which is I<implemented> as ahash itself, the only problem one has to circumvent is how to accessthis I<actual> hash (as opposed to the I<virtual> hash exhibited by theoverloaded 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 extralevel 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 C<$baz> is a reference to areference to the intermediate array, which keeps a reference to anactual array, and the access hash. The tie()ing object for the accesshash is a reference to a reference to the actual array, so=over=item *There are no loops of references.=item *Both "objects" which are blessed into the class C<two_refs1> arereferences to a reference to an array, thus references to a I<scalar>.Thus the accessor expression C<$$foo-E<gt>[$ind]> involves nooverloaded operations.=back=head2 Symbolic calculatorPut this in F<symbolic.pm> in your Perl library directory: 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 notprovide any usual overloaded operators, instead it provides the L<LastResort> operator C<nomethod>. In this example the correspondingsubroutine returns an object which encapsulates operations done overthe objects: C<new symbolic 3> contains C<['n', 3]>, C<2 + newsymbolic 3> contains C<['+', 2, ['n', 3]]>.Here is an example of the script which "calculates" the side ofcircumscribed 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 I<use> this value. In fact this value maybe inspected in debugger (see L<perldebug>), but ony ifC<bareStringify> B<O>ption is set, and not via C<p> command.If one attempts to print this value, then the overloaded operatorC<""> will be called, which will call C<nomethod> operator. Theresult of this operator will be stringified again, but this result isagain of type C<symbolic>, which will lead to an infinite loop.Add a pretty-printer method to the module F<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 C<pretty> is doing object-to-string conversion, so itis natural to overload the operator C<""> using this method. However,inside such a method it is not necessary to pretty-print theI<components> $a and $b of an object. In the above subroutineC<"[$meth $a $b]"> is a catenation of some strings and components $aand $b. If these components use overloading, the catenation operatorwill look for an overloaded operator C<.>; if not present, it willlook for an overloaded operator C<"">. 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 possiblemethods.Something is is still amiss: consider the loop variable $cnt of thescript. It was a number, not an object. We cannot make this value oftype C<symbolic>, since then the loop will not terminate.Indeed, to terminate the cycle, the $cnt should become false.However, the operator C<bool> for checking falsity is overloaded (thistime via overloaded C<"">), and returns a long string, thus any objectof type C<symbolic> is true. To overcome this, we need a way tocompare an object to 0. In fact, it is easier to write a numericconversion routine.Here is the text of F<symbolic.pm> with such a routine added (andslightly 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(). Ofcourse, %subr is not complete, it contains only operators used in theexample below. Here is the extra-credit question: why do we need anexplicit 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.182598The above module is very primitive. It does not implementmutator methods (C<++>, C<-=> and so on), does not do deep copying(not required without mutators!), and implements only those arithmeticoperations which are used in the example.To implement most arithmetic operations is easy; one should just usethe 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 L<Calling Conventions for Mutators>, we do not need anythingspecial to make C<+=> and friends work, except filling C<+=> entry of%subr, and defining a copy constructor (needed since Perl has noway to know that the implementation of C<'+='> does not mutatethe argument, compare L<Copy Constructor>).To implement a copy constructor, add C<< '=' => \&cpy >> to C<use overload>line, and code (this code assumes that mutators change things one leveldeep only, so recursive copying is not needed): sub cpy { my $self = shift; bless [@$self], ref $self; }To make C<++> and C<--> work, we need to implement actual mutators,either directly, or in C<nomethod>. We continue to do things insideC<nomethod>, thus add if ($meth eq '++' or $meth eq '--') { @$obj = ($meth, (bless [@$obj]), 1); # Avoid circular reference return $obj; }after the first line of wrap(). This is not a most effectiveimplementation, 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 in50 lines of Perl code. Since the numeric values of subexpressionsare not cached, the calculator is very slow.Here is the answer for the exercise: In the case of str(), we need noexplicit recursion since the overloaded C<.>-operator will fall backto an existing overloaded operator C<"">. Overloaded arithmeticoperators I<do not> fall back to numeric conversion if C<fallback> isnot explicitly requested. Thus without an explicit recursion num()would convert C<['+', $a, $b]> to C<$a + $b>, which would just rebuildthe argument of num().If you wonder why defaults for conversion are different for str() andnum(), note how easy it was to write the symbolic calculator. Thissimplicity is due to an appropriate choice of defaults. One extranote: 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.=head2 I<Really> symbolic calculatorOne may wonder why we call the above calculator symbolic. The reasonis that the actual calculation of the value of expression is postponeduntil the value is I<used>.To see it in action, add a method sub STORE { my $obj = shift; $#$obj = 1; @$obj->[0,1] = ('=', shift); }to the package C<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 modulesymbolic provides a I<symbolic> calculator indeed.To hide the rough edges under the hood, provide a tie()d interface to thepackage C<symbolic> (compare with L<Metaphor clash>). Add methods sub TIESCALAR { my $pack = shift; $pack->new(@_) } sub FETCH { shift } sub nop { } # Around a bug(the bug is described in L<"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 C<$a = 12; $b = 5> the numeric valueof $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 $aand $b.=head1 AUTHORIlya Zakharevich E<lt>F<ilya@math.mps.ohio-state.edu>E<gt>.=head1 DIAGNOSTICSWhen Perl is run with the B<-Do> switch or its equivalent, overloadinginduces diagnostic messages.Using the C<m> command of Perl debugger (see L<perldebug>) one candeduce which operations are overloaded (and which ancestor triggersthis overloading). Say, if C<eq> is overloaded, then the method C<(eq>is shown by debugger. The method C<()> corresponds to the C<fallback>key (in fact a presence of this method shows that this package hasoverloading enabled, and it is what is used by the C<Overloaded>function of module C<overload>).The module might issue the following warnings:=over 4=item Odd number of arguments for overload::constant(W) The call to overload::constant contained an odd number of arguments.The arguments should come in pairs.=item `%s' is not an overloadable type(W) You tried to overload a constant type the overload package is unaware of.=item `%s' is not a code reference(W) The second (fourth, sixth, ...) argument of overload::constant needsto be a code reference. Either an anonymous subroutine, or a referenceto a subroutine.=back=head1 BUGSBecause it is used for overloading, the per-package hash %OVERLOAD nowhas a special meaning in Perl. The symbol table is filled with nameslooking like line-noise.For the purpose of inheritance every overloaded package behaves as ifC<fallback> is present (possibly undefined). This may createinteresting effects if some package is not overloaded, but inheritsfrom two overloaded packages.Relation between overloading and tie()ing is broken. Overloading istriggered or not basing on the I<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 thetie()d value does not change, a simple workaround is to access the valueimmediately after tie()ing, so that after this call the I<previous> classcoincides with the current one.B<Needed:> a way to fix this without a speed penalty.Barewords are not covered by overloaded string constants.This document is confusing. There are grammos and misleading languageused in places. It would seem a total rewrite is needed.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -