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 824261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:04:30+00:00 2026-05-15T03:04:30+00:00

I have a class with several variables, one of which is a hash (_runs):

  • 0

I have a class with several variables, one of which is a hash (_runs):

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

Now, all I’m trying to do is create an accessor/mutator, as well as another subroutine that pushes new data into the hash. But I’m having a hell of a time getting all the referencing/dereferencing/$self calls working together. I’ve about burned my eyes out with “Can’t use string (“blah”) as a HASH ref etc etc” errors.

For the accessor, what is ‘best practice’ for returning hashes? Which one of these options should I be using (if any)?:

return $self->{_runs};
return %{ $self->{_runs} };
return \$self->{_runs};

Further, when I’m using the hash within other subroutines in the class, what syntax do I use to copy it?

my @runs = $self->{_runs};
my @runs = %{ $self->{_runs} };
my @runs = $%{ $self->{_runs} };
my @runs = $$self->{_runs};

Same goes for iterating over the keys:

foreach my $dt (keys $self->{_runs})
foreach my $dt (keys %{ $self->{_runs} })

And how about actually adding the data?

$self->{_runs}{$dt} = $duration;
%{ $self->{_runs} }{$dt} = $duration;
$$self->{_runs}{$dt} = $duration;

You get the point. I’ve been reading articles about using classes, and articles about referencing and dereferencing, but I can’t seem to get my brain to combine the knowledge and use both at the same time. I got my _times array working finally, but mimicking my array syntax over to hashes didn’t work.

  • 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-15T03:04:30+00:00Added an answer on May 15, 2026 at 3:04 am

    You are storing references to array or hashes in your object. To use them with standard functions you’ll need to dereference them. For example:

    @{ $self->{_array_ref_key} }; 
    %{ $self->{_hash_ref_key} };
    

    If you need pass parameters to standard function:

    push( @{ $self->{_array_ref_key} }, $some_value );
    for my $hash_key ( keys %{ $self->{_hash_ref_key} }) {
        $self->{_hash_ref_key}{$hash_key}; ## you can access hash value by reference
    }
    

    Also $self->{_hash_ref_key}{$hash_key} syntax is shortcut for $self->{_hash_ref_key}->{$hash_key} (which can make for sense if you see it first time).

    Also take a look at corresponding manual page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with several member variables, one of which is a DefaultTableModel.
I have one main class with several inherited members who all overload the same
It seems I have a MATLAB name clash with one of my variables called
I have several divs with one class on a page. <div class=main/> <div class=main/>
I have a list of objects which I built with a class, and one
I have a table containing several columns, one of which is a date column
I have a class containing several properties (all are strings if it makes any
I have a class that has several member classes as attributes. The constructor of
I have a class library containing several structures each consisting of several value and
I have a class that currently has several methods that take integer parameters. These

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.