Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Overloaded operator ends up with three arguments - SOLVED

by gnosti (Friar)
on Mar 31, 2013 at 06:56 UTC ( #1026336=perlquestion: print w/ replies, xml ) Need Help??
gnosti has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I've written a class Lat for latency objects, having min and max values. To sum them I overload '+', pointing to \&add_latency. The overloaded addition fails, due to an extra undefined argument getting into add_latency()'s @_.

Thank you kindly for your attention!

Synopsis

my $lat1 = Lat->new(4,8); my $lat2 = Lat->new(16,20); my $okay_sum = $lat1->add_latency($lat2); # this works my $failed_sum = $lat1 + $lat2 # overloaded op, gets extra arg

Below is a test case. I'd like help finding where the third argument comes from. Thanks for your attention.

Any other critique of my code is welcome.

Test code

package Lat; use Modern::Perl; our @ISA; use Data::Dumper::Concise; use overload '+' => \&add_latency, "\"\"" => sub { join ' ',$_[0]->min, $_[0]->max }; sub new { my $class = shift; my ($min, $max) = @_; die "Lat object has Min ($min) greater than Max ($max)" if $min > +$max; my $self = bless [$min, $max], $class; $self; } sub add_latency { my (@latencies) = @_; say "found ",scalar @latencies, " latency objects"; my $i; map{say "Addend ",++$i, "\n", Dumper $_} @latencies; my ($min, $max) = (0,0); map{ $min += $_->min; $max += $_->max } @latencies; Lat->new($min, $max); } sub min {$_[0]->[0] } sub max {$_[0]->[1] } 1; package main; use Test::More qw(no_plan); use strict; use Data::Dumper::Concise; #use Lat; my $lat = Lat->new(4,8); my $lat2 = Lat->new(16,32); is(ref $lat, 'Lat', "Latency object instantiation"); is("$lat","4 8","Stringify object"); is($lat->min, 4, "Min latency accessor"); is_deeply( $lat->add_latency($lat2), Lat->new(20,40), "Latency additio +n"); is_deeply( Lat->new(20,40), ($lat + $lat2), "Latency addition, overloa +ding '+' operator"); 1; __END__ ok 1 - Latency object instantiation ok 2 - Stringify object ok 3 - Min latency accessor found 2 latency objects Addend 1 bless( [ 4, 8 ], 'Lat' ) Addend 2 bless( [ 16, 32 ], 'Lat' ) ok 4 - Latency addition found 3 latency objects Addend 1 bless( [ 4, 8 ], 'Lat' ) Addend 2 bless( [ 16, 32 ], 'Lat' ) Addend 3 "" Can't call method "min" without a package or object reference at test- +over2 line 21. 1..4 # Looks like your test exited with 2 just after 4.

Comment on Overloaded operator ends up with three arguments - SOLVED
Select or Download Code
Re: Overloaded operator ends up with three arguments.
by BrowserUk (Pope) on Mar 31, 2013 at 07:09 UTC

    See Calling-Conventions-for-Binary-Operations that states that binary operators are called with three arguments; the third being a boolean indicating the ordering of the operands.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you. I will try to RTFM more thoroughly next time.
Re: Overloaded operator ends up with three arguments. (as documented)
by Anonymous Monk on Mar 31, 2013 at 07:13 UTC

    Your mistake is here  use overload '+' => \&add_latency,

    add_latency is not written to cooperate with overload

    As overload examples  my ($self, $other, $swap) = @_;

    To explain, + is a binop, binary operator, it takes two arguments ... this arglist is slightly clearer

    my( $self_operand_overloaded, $other_operand, $other_operand_was_on_the_left_side ) = @_;

    $other_operand can be undef

    $other_operand_was_on_the_left_side is a boolean, not an object

    overload explains in detail

      Thanks for clarifying this.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1026336]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-09-22 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls