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.