CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Overloaded += operator works here, fails here



May 8, 2013, 10:18 PM

Post #1 of 3 (1097 views)
Overloaded += operator works here, fails here Can't Post

Greetings again.

As usual, I have a weird problem. I am overloading operators +, +=, -, and =-.

Taking the easy way out, the base operation is += and my + handler calls +=. This works in testing.

Now I try the same scheme for subtraction starting with the -= handler, with one extra step:
The -= handler
1. Clones a copy of the second operand
2. Negates the copy. (I have proven this part works correctly, using the x command in the debugger)
3. Uses $self += the negated object.

Now step 3 is used by the + handler and works just fine; my tests have proven this.

Enough abstraction; Here is code:

The + handler code:

755 sub Yapp_add 
756 {
757 my ($self, $adder, $swapped) = @_; #(swap is irrelelvant for add)
759 my $ryapp = yapp($self); # Clone the original
760 $ryapp += $adder; # Let yapp_plus() function handle details
761 return $ryapp; # Assume $ryapp will be auto-destroyed?
762 }

As I said, this passed several tests.

Now for the -= code, called by the - handler. The relevant part, around line 800, looks very much like the above.

793 sub Yapp_minus 
794 {
795 my ($self, $subtractor) = @_[0 .. 1];
797 if (ref($subtractor) eq $class_name) # Subtracting another Yapp
798 { # just use the add method
799 my $temp_yapp = $subtractor->Yapp_negate(); # Quickie way out: Negate and
800 $self += $temp_yapp; # Add the negated Yapp
801 }
802 else # Otherwise, just assume a constant
803 { # be it real or complex.
804 # If our Yapp has no constant term, this is it, after negation.
805 #
806 $self->{coeff}[0] = (defined($self->{coeff}[0]))
807 ? $self->{coeff}[0] - $subtractor
808 : - $subtractor ;
809 }
810 return $self;
811 }

The important code is at line 800. When I try to execute this, I get a bizarre message:

Operation "=": no method found, argument in overloaded package Math::Yapp at Math/ line 800. 
at Math/ line 800

(Yeah, some kind of vector space stuff. That's not important to my problem, though.)

So why does it work so nicely in the add (+) handler at line 760 and barf in the -= handler at line 800? Why does it suddenly find no = handler when it had no problem doing that earlier?

Curiouser and curiouser! Crazy
-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)


May 8, 2013, 10:52 PM

Post #2 of 3 (1094 views)
Re: [rpaskudniak] Overloaded += operator works here, fails here [In reply to] Can't Post

Looking at my post, I noticed a miniscule difference between the code that worked and the code that did not:

In the code that was failing, in line 800, the target of the += operator was $self, the object whose of the -= call of this function. On a lark, I
- Cloned an additional copy of $self, named $temp_self
- Performed the identical += operation that I was trying
- Copied the relevant, modified portion of $temp_self back to $self.

The operator now works.

I'd like to mark this solved but I am still bothered: What is the rationale for forbidding me to use the overloaded operator (or any mutator) on the $self object? The error message indicates that's not quite what I violated but I would appreciate an explanation of Perl's objection to it.

-- Rasputin Paskudniak (In perpetual pursuit of undomesticated, semi-aquatic avians)


May 20, 2013, 11:40 PM

Post #3 of 3 (1037 views)
Re: [rpaskudniak] Overloaded += operator works here, fails here [In reply to] Can't Post

I suggest you post the relevant declarations from Math::Yapp too, i.e.

- The declaration for the overload operators, and
- The declaration for inheritance

otherwise it is hard to judge what's going wrong. For sure, Perl doesn't make an exception for variables called "$self"....


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives