1 Replies - 4930 Views - Last Post: 29 August 2013 - 01:33 AM

#1 Arkwind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-November 08

Private Hash Reference In Object

Posted 28 August 2013 - 09:47 AM

Hi there,

So basically I need a way to use hash references as a private attribute of an object (if that makes sense).

What i am after is something like this:


sub new
{
    my $class = shift;
  	my $self = {
	    _RandomOrRecall => shift,
            _NumberOfInputs => shift,
            _Role => shift,
            _Tools => shift,
            _MyHashRef => {},
   	};

    bless $self, $class;


	
    return($self);
}

....




Then store and refer to a dynamic reference within that private hash later in a method like this:


$self->{_MyHashRef}->{"Name"} = "Danny Boy"
$self->{_MyHashRef}->{"Age"} = "18"




What is the correct way to do this or am I on the right lines?

Is This A Good Question/Topic? 0
  • +

Replies To: Private Hash Reference In Object

#2 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Private Hash Reference In Object

Posted 29 August 2013 - 01:33 AM

Perl is great for just trying things like that to see if they work, but yeah, it looks right to me.

One minor tip: Using -> is optional after the first one, so you can write just $self->{_MyHashRef}{Name} = "Danny Boy" if you want to be a bit lazier.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1