Subject: | Data::Average push vs. foreach loop, data method, average export and documentation |
This foreach loop is much better written as push
< $self->{data}->[ $self->length ] = $_ for @_;
Show quoted text
> push @{$self->{data}}, @_;
Documentation:
'You may add objects to the array if they support a "value" method.'
'You can also construct a new object as
my $average=Data::Average->new(data=>[1..100])->avg;
'
You also assume that $self->{data} is an array reference. You should
map this with a method you ensure that it really is an array.
=head2 data
Returns an array reference with all of the objects and/or values.
=cut
sub data {
my $self=shift;
$self->{"data"}=[] unless ref($self->{"data"}) eq "ARRAY";
return $self->{"data"};
}
I'd like to see a function "average" exported
sub average {
my $data=shift; #assume [] ???
return __PACKAGE__->new(data=>$data)->avg;
}
If you don't do the export, I most likely will as that is what I need.
Thanks,
Mike
mrdvt92