From: Nick Ing-Simmons Date: Sat, 2 Sep 2000 17:25:20 +0000 (+0000) Subject: More %{} and other deref special casing - do not pass to 'nomethod'. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b267980ddb40b015c095b6e55ef52f73a92b67b0;p=p5sagit%2Fp5-mst-13.2.git More %{} and other deref special casing - do not pass to 'nomethod'. p4raw-id: //depot/perl@7012 --- diff --git a/gv.c b/gv.c index 3787f1b..768824d 100644 --- a/gv.c +++ b/gv.c @@ -159,18 +159,18 @@ S_gv_init_sv(pTHX_ GV *gv, I32 sv_type) Returns the glob with the given C and a defined subroutine or C. The glob lives in the given C, or in the stashes -accessible via @ISA and @UNIVERSAL. +accessible via @ISA and @UNIVERSAL. The argument C should be either 0 or -1. If C, as a side-effect creates a glob with the given C in the given C which in the case of success contains an alias for the subroutine, and sets -up caching info for this glob. Similarly for all the searched stashes. +up caching info for this glob. Similarly for all the searched stashes. This function grants C<"SUPER"> token as a postfix of the stash name. The GV returned from C may be a method cache entry, which is not visible to Perl code. So when calling C, you should not use the GV directly; instead, you should use the method's CV, which can be -obtained from the GV with the C macro. +obtained from the GV with the C macro. =cut */ @@ -317,24 +317,24 @@ Perl_gv_fetchmethod(pTHX_ HV *stash, const char *name) Returns the glob which contains the subroutine to call to invoke the method on the C. In fact in the presence of autoloading this may be the glob for "AUTOLOAD". In this case the corresponding variable $AUTOLOAD is -already setup. +already setup. The third parameter of C determines whether AUTOLOAD lookup is performed if the given method is not present: non-zero -means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD. +means yes, look for AUTOLOAD; zero means no, don't look for AUTOLOAD. Calling C is equivalent to calling C -with a non-zero C parameter. +with a non-zero C parameter. These functions grant C<"SUPER"> token as a prefix of the method name. Note that if you want to keep the returned glob for a long time, you need to check for it being "AUTOLOAD", since at the later time the call may load a different subroutine due to $AUTOLOAD changing its value. Use the glob -created via a side effect to do this. +created via a side effect to do this. These functions have the same side-effects and as C with C. C should be writable if contains C<':'> or C<' ''>. The warning against passing the GV returned by C to -C apply equally to these functions. +C apply equally to these functions. =cut */ @@ -346,7 +346,7 @@ Perl_gv_fetchmethod_autoload(pTHX_ HV *stash, const char *name, I32 autoload) register const char *nend; const char *nsplit = 0; GV* gv; - + for (nend = name; *nend; nend++) { if (*nend == '\'') nsplit = nend; @@ -424,7 +424,7 @@ Perl_gv_autoload4(pTHX_ HV *stash, const char *name, STRLEN len, I32 method) /* * Inheriting AUTOLOAD for non-methods works ... for now. */ - if (ckWARN(WARN_DEPRECATED) && !method && + if (ckWARN(WARN_DEPRECATED) && !method && (GvCVGEN(gv) || GvSTASH(gv) != stash)) Perl_warner(aTHX_ WARN_DEPRECATED, "Use of inherited AUTOLOAD for non-method %s::%.*s() is deprecated", @@ -1088,7 +1088,7 @@ Perl_gp_ref(pTHX_ GP *gp) void Perl_gp_free(pTHX_ GV *gv) { - dTHR; + dTHR; GP* gp; if (!gv || !(gp = GvGP(gv))) @@ -1128,7 +1128,7 @@ Perl_gp_free(pTHX_ GV *gv) AV *GvAVn(gv) register GV *gv; { - if (GvGP(gv)->gp_av) + if (GvGP(gv)->gp_av) return GvGP(gv)->gp_av; else return GvGP(gv_AVadd(gv))->gp_av; @@ -1200,7 +1200,7 @@ Perl_Gv_AMupdate(pTHX_ HV *stash) for (i = 1; i < NofAMmeth; i++) { cv = 0; cp = (char *)PL_AMG_names[i]; - + svp = (SV**)hv_fetch(hv, cp, strlen(cp), FALSE); if (svp && ((sv = *svp) != &PL_sv_undef)) { switch (SvTYPE(sv)) { @@ -1270,19 +1270,19 @@ Perl_Gv_AMupdate(pTHX_ HV *stash) /* GvSV contains the name of the method. */ GV *ngv; - DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n", + DEBUG_o( Perl_deb(aTHX_ "Resolving method `%.256s' for overloaded `%s' in package `%.256s'\n", SvPV(GvSV(gv), n_a), cp, HvNAME(stash)) ); - if (!SvPOK(GvSV(gv)) + if (!SvPOK(GvSV(gv)) || !(ngv = gv_fetchmethod_autoload(stash, SvPVX(GvSV(gv)), FALSE))) { /* Can be an import stub (created by `can'). */ if (GvCVGEN(gv)) { - Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'", + Perl_croak(aTHX_ "Stub found while resolving method `%.256s' overloading `%s' in package `%.256s'", (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ), cp, HvNAME(stash)); } else - Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'", + Perl_croak(aTHX_ "Can't resolve method `%.256s' overloading `%s' in package `%.256s'", (SvPOK(GvSV(gv)) ? SvPVX(GvSV(gv)) : "???" ), cp, HvNAME(stash)); } @@ -1293,7 +1293,7 @@ Perl_Gv_AMupdate(pTHX_ HV *stash) GvNAME(CvGV(cv))) ); filled = 1; } -#endif +#endif amt.table[i]=(CV*)SvREFCNT_inc(cv); } if (filled) { @@ -1313,8 +1313,8 @@ SV* Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) { dTHR; - MAGIC *mg; - CV *cv; + MAGIC *mg; + CV *cv; CV **cvp=NULL, **ocvp=NULL; AMT *amtp, *oamtp; int fl=0, off, off1, lr=0, assign=AMGf_assign & flags, notfound=0; @@ -1322,10 +1322,10 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) HV* stash; if (!(AMGf_noleft & flags) && SvAMAGIC(left) && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(left))),'c')) - && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) + && (ocvp = cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) ? (oamtp = amtp = (AMT*)mg->mg_ptr)->table : (CV **) NULL)) - && ((cv = cvp[off=method+assignshift]) + && ((cv = cvp[off=method+assignshift]) || (assign && amtp->fallback > AMGfallNEVER && /* fallback to * usual method */ (fl = 1, cv = cvp[off=method])))) { @@ -1361,7 +1361,7 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) (void)((cv = cvp[off=numer_amg]) || (cv = cvp[off=bool__amg])); break; case not_amg: - (void)((cv = cvp[off=bool__amg]) + (void)((cv = cvp[off=bool__amg]) || (cv = cvp[off=numer_amg]) || (cv = cvp[off=string_amg])); postpr = 1; @@ -1386,7 +1386,7 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) } break; case abs_amg: - if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg]) + if ((cvp[off1=lt_amg] || cvp[off1=ncmp_amg]) && ((cv = cvp[off=neg_amg]) || (cv = cvp[off=subtr_amg]))) { SV* nullsv=sv_2mortal(newSViv(0)); if (off1==lt_amg) { @@ -1417,13 +1417,16 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) } break; case iter_amg: /* XXXX Eventually should do to_gv. */ + /* FAIL safe */ + return NULL; /* Delegate operation to standard mechanisms. */ + break; case to_sv_amg: case to_av_amg: case to_hv_amg: case to_gv_amg: case to_cv_amg: /* FAIL safe */ - return NULL; /* Delegate operation to standard mechanisms. */ + return left; /* Delegate operation to standard mechanisms. */ break; default: goto not_found; @@ -1431,14 +1434,14 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) if (!cv) goto not_found; } else if (!(AMGf_noright & flags) && SvAMAGIC(right) && (mg = mg_find((SV*)(stash=SvSTASH(SvRV(right))),'c')) - && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) + && (cvp = (AMT_AMAGIC((AMT*)mg->mg_ptr) ? (amtp = (AMT*)mg->mg_ptr)->table : (CV **) NULL)) && (cv = cvp[off=method])) { /* Method for right * argument found */ lr=1; - } else if (((ocvp && oamtp->fallback > AMGfallNEVER - && (cvp=ocvp) && (lr = -1)) + } else if (((ocvp && oamtp->fallback > AMGfallNEVER + && (cvp=ocvp) && (lr = -1)) || (cvp && amtp->fallback > AMGfallNEVER && (lr=1))) && !(flags & AMGf_unary)) { /* We look for substitution for @@ -1471,6 +1474,16 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) } } else { not_found: /* No method found, either report or croak */ + switch (method) { + case to_sv_amg: + case to_av_amg: + case to_hv_amg: + case to_gv_amg: + case to_cv_amg: + /* FAIL safe */ + return left; /* Delegate operation to standard mechanisms. */ + break; + } if (ocvp && (cv=ocvp[nomethod_amg])) { /* Call report method */ notfound = 1; lr = -1; } else if (cvp && (cv=cvp[nomethod_amg])) { @@ -1478,22 +1491,22 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) } else { SV *msg; if (off==-1) off=method; - msg = sv_2mortal(Perl_newSVpvf(aTHX_ + msg = sv_2mortal(Perl_newSVpvf(aTHX_ "Operation `%s': no method found,%sargument %s%s%s%s", PL_AMG_names[method + assignshift], (flags & AMGf_unary ? " " : "\n\tleft "), - SvAMAGIC(left)? + SvAMAGIC(left)? "in overloaded package ": "has no overloaded magic", - SvAMAGIC(left)? + SvAMAGIC(left)? HvNAME(SvSTASH(SvRV(left))): "", - SvAMAGIC(right)? + SvAMAGIC(right)? ",\n\tright argument in overloaded package ": - (flags & AMGf_unary + (flags & AMGf_unary ? "" : ",\n\tright argument has no overloaded magic"), - SvAMAGIC(right)? + SvAMAGIC(right)? HvNAME(SvSTASH(SvRV(right))): "")); if (amtp && amtp->fallback >= AMGfallYES) { @@ -1507,7 +1520,7 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) } } if (!notfound) { - DEBUG_o( Perl_deb(aTHX_ + DEBUG_o( Perl_deb(aTHX_ "Overloaded operator `%s'%s%s%s:\n\tmethod%s found%s in package %s%s\n", PL_AMG_names[off], method+assignshift==off? "" : @@ -1518,7 +1531,7 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) flags & AMGf_unary? "" : lr==1 ? " for right argument": " for left argument", flags & AMGf_unary? " for argument" : "", - HvNAME(stash), + HvNAME(stash), fl? ",\n\tassignment variant used": "") ); } /* Since we use shallow copy during assignment, we need @@ -1531,10 +1544,10 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags) * b) Increment or decrement, called directly. * assignshift==0, assign==0, method + 0 == off * c) Increment or decrement, translated to assignment add/subtr. - * assignshift==0, assign==T, + * assignshift==0, assign==T, * force_cpy == T * d) Increment or decrement, translated to nomethod. - * assignshift==0, assign==0, + * assignshift==0, assign==0, * force_cpy == T * e) Assignment form translated to nomethod. * assignshift==1, assign==T, method + 1 != off diff --git a/lib/overload.pm b/lib/overload.pm index 6b1b82f..bead929 100644 --- a/lib/overload.pm +++ b/lib/overload.pm @@ -106,7 +106,7 @@ sub mycan { # Real can would leave stubs. } %constants = ( - 'integer' => 0x1000, + 'integer' => 0x1000, 'float' => 0x2000, 'binary' => 0x4000, 'q' => 0x8000, @@ -167,7 +167,7 @@ sub remove_constant { __END__ -=head1 NAME +=head1 NAME overload - Package for overloading perl operations @@ -175,7 +175,7 @@ overload - Package for overloading perl operations package SomeThing; - use overload + use overload '+' => \&myadd, '-' => \&mysub; # etc @@ -197,12 +197,12 @@ The compilation directive package Number; use overload - "+" => \&add, + "+" => \&add, "*=" => "muas"; declares function Number::add() for addition, and method muas() in the "class" C (or one of its base classes) -for the assignment form C<*=> of multiplication. +for the assignment form C<*=> of multiplication. Arguments of this directive come in (key, value) pairs. Legal values are values legal inside a C<&{ ... }> call, so the name of a @@ -297,20 +297,20 @@ if C<+=> is not overloaded. =back B 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 +routines which may be called in assignment context may create +self-referential structures. Currently Perl will not free self-referential structures until cycles are C broken. You may get problems when traversing your structures too. -Say, +Say, use overload '+' => sub { bless [ \$_[0], \$_[1] ] }; is asking for trouble, since for code C<$obj += $foo> the subroutine -is called as C<$obj = add($obj, $foo, undef)>, or C<$obj = [\$obj, +is called as C<$obj = add($obj, $foo, undef)>, or C<$obj = [\$obj, \$foo]>. If using such a subroutine is an important optimization, one can overload C<+=> explicitly by a non-"optimized" version, or switch -to non-optimized version if C (see +to non-optimized version if C (see L). Even if no I assignment-variants of operators are present in @@ -400,6 +400,12 @@ If not overloaded, the argument will be dereferenced I, 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". + =item * I "nomethod", "fallback", "=", @@ -482,11 +488,16 @@ the last one is used. Say, C<1-$a> can be equivalent to if the pair C<"nomethod" =E "nomethodMethod"> was specified in the C directive. +The C<"nomethod"> mechanism is I used for the dereference operators +( ${} @{} %{} &{} *{} ). + + If some operation cannot be resolved, and there is no function assigned to C<"nomethod">, then an exception will be raised via die()-- unless C<"fallback"> was specified as a key in C directive. -=head2 Fallback + +=head2 Fallback The key C<"fallback"> governs what to do if a method for a particular operation is not found. Three different cases are possible depending on @@ -510,7 +521,7 @@ present. =item * defined, but FALSE No autogeneration is tried. Perl tries to call -C<"nomethod"> value, and if this is missing, raises an exception. +C<"nomethod"> value, and if this is missing, raises an exception. =back @@ -528,7 +539,7 @@ This operation is called in the situations when a mutator is applied to a reference that shares its object with some other reference, such as - $a=$b; + $a=$b; ++$a; To make this change $a and not change $b, a copy of C<$$a> is made, @@ -539,7 +550,7 @@ done if C<++> is expressed via a method for C<'++'> or C<'+='> (or C). Note that if this operation is expressed via C<'+'> a nonmutator, i.e., as in - $a=$b; + $a=$b; $a=$a+1; then C<$a> does not reference a new copy of C<$$a>, since $$a does not @@ -553,15 +564,15 @@ string copy if the object is a plain scalar. =item B -The actually executed code for +The actually executed code for - $a=$b; + $a=$b; Something else which does not modify $a or $b.... ++$a; may be - $a=$b; + $a=$b; Something else which does not modify $a or $b.... $a = $a->clone(undef,""); $a->incr(undef,""); @@ -588,7 +599,7 @@ substitutions are possible for the following operations: C<$a+=$b> can use the method for C<"+"> if the method for C<"+="> is not defined. -=item I +=item I String, numeric, and boolean conversion are calculated in terms of one another if not all of them are defined. @@ -615,7 +626,7 @@ string or numerical conversion. can be expressed in terms of string conversion. -=item I +=item I can be expressed in terms of its "spaceship" counterpart: either C=E> or C: @@ -723,20 +734,20 @@ to overload constant pieces of regular expressions. The corresponding values are references to functions which take three arguments: the first one is the I string form of the constant, the second one -is how Perl interprets this constant, the third one is how the constant is used. +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 +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 +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 C- and C- constants, it is C in single-quote context (comes from strings, regular expressions, and single-quote HERE -documents), it is C for arguments of C/C operators, +documents), it is C for arguments of C/C operators, it is C for right-hand side of C-operator, and it is C otherwise. Since an expression C<"ab$cd,,"> is just a shortcut for C<'ab' . $cd . ',,'>, it is expected that overloaded constant strings are equipped with reasonable -overloaded catenation operator, otherwise absurd results will result. +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() @@ -750,7 +761,7 @@ From these methods they may be called as overload::constant integer => sub {Math::BigInt->new(shift)}; } -B Currently overloaded-ness of constants does not propagate +B Currently overloaded-ness of constants does not propagate into C. =head1 IMPLEMENTATION @@ -792,7 +803,7 @@ packages acquire a magic during the next Cing into the package. This magic is three-words-long for packages without overloading, and carries the cache table if the package is overloaded. -Copying (C<$a=$b>) is shallow; however, a one-level-deep copying is +Copying (C<$a=$b>) is shallow; however, a one-level-deep copying is carried out before any operation that can imply an assignment to the object $a (or $b) refers to, like C<$a++>. You can override this behavior by defining your own copy constructor (see L<"Copy Constructor">). @@ -803,8 +814,8 @@ to be changed are constant (but this is not enforced). =head1 Metaphor clash One may wonder why the semantic of overloaded C<=> is so counter intuitive. -If it I counter intuitive to you, you are subject to a metaphor -clash. +If it I counter intuitive to you, you are subject to a metaphor +clash. Here is a Perl object metaphor: @@ -823,10 +834,10 @@ 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 +would change the data referenced by $b too. Effectively, after C<$a = $b> values of $a and $b become I. -On the other hand, anyone who has used algebraic notation knows the +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 @@ -835,7 +846,7 @@ far as all the mutators are called via overloaded access only>. The way it is done is described in L. If some mutator methods are directly applied to the overloaded values, -one may need to I other values which references the +one may need to I other values which references the same value: $a = new Data 23; @@ -859,7 +870,7 @@ However, it would not make preserve "objectness" of $a. But Perl I a way to make assignments to an object do whatever you want. It is just not the overload, but tie()ing interface (see L). Adding a FETCH() method -which returns the object itself, and STORE() method which changes the +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. @@ -903,8 +914,8 @@ allowing index 0 to be treated as a normal element. package two_refs; use overload '%{}' => \&gethash, '@{}' => sub { $ {shift()} }; - sub new { - my $p = shift; + sub new { + my $p = shift; bless \ [@_], $p; } sub gethash { @@ -918,13 +929,13 @@ allowing index 0 to be treated as a normal element. my %fields; my $i = 0; $fields{$_} = $i++ foreach qw{zero one two three}; - sub STORE { + sub STORE { my $self = ${shift()}; my $key = $fields{shift()}; defined $key or die "Out of band access"; $$self->[$key] = shift; } - sub FETCH { + sub FETCH { my $self = ${shift()}; my $key = $fields{shift()}; defined $key or die "Out of band access"; @@ -957,7 +968,7 @@ 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 %{} + bless $self, 'overload::dummy'; # Disable overloading of %{} my $out = $self->{$key}; bless $self, $class; # Restore overloading $out; @@ -969,8 +980,8 @@ 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; + sub new { + my $p = shift; my $a = [@_]; my %h; tie %h, $p, $a; @@ -987,13 +998,13 @@ level of indirection which allows a non-circular structure of references: my %fields; my $i = 0; $fields{$_} = $i++ foreach qw{zero one two three}; - sub STORE { + sub STORE { my $a = ${shift()}; my $key = $fields{shift()}; defined $key or die "Out of band access"; $a->[$key] = shift; } - sub FETCH { + sub FETCH { my $a = ${shift()}; my $key = $fields{shift()}; defined $key or die "Out of band access"; @@ -1003,7 +1014,7 @@ level of indirection which allows a non-circular structure of references: Now if $baz is overloaded like this, then C<$bar> 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 also a reference to a reference to the actual array, so +hash is also a reference to a reference to the actual array, so =over @@ -1078,7 +1089,7 @@ Add a pretty-printer method to the module F: $a = $a->pretty if ref $a; $b = $b->pretty if ref $b; "[$meth $a $b]"; - } + } Now one can finish the script by @@ -1099,7 +1110,7 @@ look for an overloaded operator C<"">. Thus it is enough to use $a = 'u' unless defined $a; $b = 'u' unless defined $b; "[$meth $a $b]"; - } + } Now one can change the last line of the script to @@ -1110,7 +1121,7 @@ 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. +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 @@ -1144,9 +1155,9 @@ slightly modified str()): } else { "[$meth $a]"; } - } - my %subr = ( n => sub {$_[0]}, - sqrt => sub {sqrt $_[0]}, + } + my %subr = ( n => sub {$_[0]}, + sqrt => sub {sqrt $_[0]}, '-' => sub {shift() - shift()}, '+' => sub {shift() + shift()}, '/' => sub {shift() / shift()}, @@ -1155,7 +1166,7 @@ slightly modified str()): ); sub num { my ($meth, $a, $b) = @{+shift}; - my $subr = $subr{$meth} + 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__; @@ -1224,7 +1235,7 @@ deep only, so recursive copying is not needed): bless [@$self], ref $self; } -To make C<++> and C<--> work, we need to implement actual mutators, +To make C<++> and C<--> work, we need to implement actual mutators, either directly, or in C. We continue to do things inside C, thus add @@ -1233,7 +1244,7 @@ C, thus add return $obj; } -after the first line of wrap(). This is not a most effective +after the first line of wrap(). This is not a most effective implementation, one may consider sub inc { $_[0] = bless ['++', shift, 1]; } @@ -1256,8 +1267,8 @@ As a final remark, note that one can fill %subr by $subr{'++'} = $subr{'+'}; $subr{'--'} = $subr{'-'}; -This finishes implementation of a primitive symbolic calculator in -50 lines of Perl code. Since the numeric values of subexpressions +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 @@ -1283,9 +1294,9 @@ until the value is I. To see it in action, add a method - sub STORE { - my $obj = shift; - $#$obj = 1; + sub STORE { + my $obj = shift; + $#$obj = 1; @$obj->[0,1] = ('=', shift); } @@ -1386,12 +1397,12 @@ C 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 +Relation between overloading and tie()ing is broken. Overloading is triggered or not basing on the I class of tie()d value. -This happens because the presence of overloading is checked too early, +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 +tie()d value does not change, a simple workaround is to access the value immediately after tie()ing, so that after this call the I class coincides with the current one.