[Solved] Perl on Modules-Inheritance,Polymorphism [closed]


Ok. The object constructor syntax you are using is a bit akward, I’d prefer

my $obj = Stats->new(13,4,56,43,33);

In Perl, new is not an ordinary keyword, but a simple sub, and should be used as such. The Foo->sub(@args) syntax is exactly equivalent to Foo::sub('Foo', @args), and thus takes care of passing the correct class name and calling the correct new sub.

Then, you should use the numbers you are passing to your Stats constructor. This constructor should do the trick:

sub new {
  my ($class, @args) = @_;
  my $self = {};
  bless $self, $class;
  $self->clear();
  $self->addValue($_) foreach @args;
  return $self;
}

I stuff all arguments of the constructor into the @args array and then loop over them and add these values to our stats object.

Also, do not forget to actually call results() to execute your test. It will print:

Number of values: 5
Total: 149
Mean: 29.8

0

solved Perl on Modules-Inheritance,Polymorphism [closed]