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.

In reply to Overloaded operator ends up with three arguments - SOLVED by gnosti

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.