Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:53:33+00:00 2026-06-12T01:53:33+00:00

This is a question from object oriented Perl. I am supposed to design a

  • 0

This is a question from object oriented Perl. I am supposed to design a module:
1)Store the values
2)Calculate the Total, Mean, Count.
I am supposed to find a code which relates method overriding or polymorphism or inheritance in Object Oriented Perl.

My code is like this:

package Stats;
use strict;
use warnings;
sub new {
  my $class = @_;
  my $self = {};
  bless $self, $class;
  $self->clear();
  return $self;
}
sub clear {
  my $self = $_[0];
  $self->{'numlist'} = undef;
  $self->{'x_sum'} = 0;
  $self->{'x2_sum'} = 0;
}
sub addValue {
  my $self = $_[0];
  my $num = $_[1];
  if (defined $num) {
    push @{$self->{'numlist'}}, $num;
    $self->{'x_sum'} += $num;
    $self->{'x2_sum'} += $num**2;
  }
}
sub getTotal {
  my $self = $_[0];
  return $self->{'x_sum'};
}
sub getMean {
  my $self = $_[0];
  my @numlist = @{$self->{'numlist'}};
  if (!@numlist) { return 0; }
  return $self->getTotal()/@numlist;
}
sub getValueList {
  my $self = $_[0];
  return @{$self->{'numlist'}};
}
1;


sub results {
my $obj = new Stats(13,4,56,43,33);
print "Number of values: ", scalar($obj->getValueList()), "\n";
print "Total: ", $obj->getTotal(), "\n";
print "Mean: ", $obj->getMean(), "\n";
}

Where am I going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:53:34+00:00Added an answer on June 12, 2026 at 1:53 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been thinking about this object oriented design question for a while now and
I have a question re object-oriented design - I have two classes, a Map
I know this looks like a silly question, but using object oriented stuff with
I've this question from an assignment to create a Store which rent out books,
This is related to my previous question , regarding pulling objects from a dmp
This is a continuation of this question from yesterday . Here are my three
I got this question from my cousin: What will be the best way to
Unfortunately I am not writing this question from my Developing PC so I might
This question results from hours of googling highstocks, zoom, extremes, ranges, and every other
This question stems from Hartl's Rails Tutorial (progressed in chapter 9) - sorry if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.