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

  • Home
  • SEARCH
  • 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 822731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:50:27+00:00 2026-05-15T02:50:27+00:00

Trying to get the hang of using OOP in Perl. My problem is that

  • 0

Trying to get the hang of using OOP in Perl. My problem is that I’m setting a variable in the class, but the value is lost when I try and retrieve it. I’m sure the issue is glaring, but I need some extra eyes.

Constructor:

sub new
{
    my ($class, $name) = @_;
    my $self = {
        _name => $name,
        _times => []
    };
    bless ($self, $class);
    return $self;
}

accessor/mutator method:

sub times {
    my ($self) = shift;
    if (@_) { @{$self->{_times}} = shift }
    print "times size: " . @{$self->{_times}} . "\n";
    return @{$self->{_times}};
}

call from the main program:

$js->addRun($duration, $curStartTime);
print "Times size: " . @{$js->times()} . "\n";

relevant code from addRun() subroutine:

sub addRun {
    my ($self, $duration, $runDateTime) = @_;
    if (!defined($duration) || !defined($runDateTime)) { return 0; }
    push(@{$self->{_times}},$duration);
}

When I run this code, it enters the addRun subroutine and pushes the value to the _times variable. Then I print the value by calling the accessor/mutator. But the accessor/mutator has its own print command, so I can check the value before I return it.

The accessor prints the correct value, but when I print what was returned, it’s undefined. Is my syntax messed up somewhere? Am I just an idiot?

Thanks

  • 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-05-15T02:50:27+00:00Added an answer on May 15, 2026 at 2:50 am

    The problem is in your times() subroutine you are returning an array, not an array reference.

    Then in your main program you are trying to dereference the call to times() but you don’t need to.

    So in your main program just call it as follows:-

    print "Times size: " . $js->times() . "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the hang of using variables in C#, but have hit
I'm trying to get the hang of using notifications. In my view controller class,
I'm still trying to get a hang of Perl's OOP features. I'm confused about
Im using xmpp4r and trying to get the hang of a basic chat feature
I'm still trying to get the hang of SQL. I built 1 query that
Just trying to get the hang of using the semantically correct XHTML markup. Just
I'm a webdesigner that's trying to get the hang of JavaScript and jQuery, and
I am trying to get the hang of retain / release. I get that
I'm trying to get the hang of UITableViews and everything that goes with it.
I am trying to get the hang of C++ pointers and objects, through a

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.