Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Hashes in Objects

by Angel (Friar)
on May 07, 2003 at 15:42 UTC ( [id://256255]=perlquestion: print w/replies, xml ) Need Help??

Angel has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I have read many things on perl objects and have made a few. But I am perplexed at how to do hashes inside the object.

To give example I want the self hash to have a hash inside it to store things. I would greatly appreciate an example constructor, and get and set methods by key and possibly how to get the hash for internal methods

sub new { my $self = {}; #error reporting and update vars $self->{'error_type'} = undef; $self->{'error_string'} = undef; $self->{'text'} = undef; $self->{'query'} = undef; $self->{'reference'} = undef; $self->{'dbh'} = undef; $self->{'tokens'} = %( how do i do this ) bless ($self); return $self; } sub set_token { my $self = shift; $self->{'token'}{'$_[0]'} = $_[1]; } sub get_token { my $self = shift; return $self->{'token'}{'$_[0]'}; } sub get_token_keys { my $self = shift; keys $self->{'token'}; }

Replies are listed 'Best First'.
Re: Hashes in Objects
by kilinrax (Deacon) on May 07, 2003 at 15:46 UTC
    Use an anonymous hash constructor to create the reference:
    $self->{'tokens'} = {};
    Have a read through perlref, it should give you a good idea of how to create more complex structures with references.
Re: Hashes in Objects
by halley (Prior) on May 07, 2003 at 15:58 UTC

    The above replies gave the answer. Here's a little more context:

    sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = { }; $self->{token} = { a => 5 }; # equivalent to $self->{token} = \%( a => 5 ); return bless($self, $class); }
    Minor changes required to your accessors:
    sub set_token { my $self = shift; $self->{token}{$_[0]} = $_[1]; } sub get_token { my $self = shift; return $self->{token}{$_[0]}; } sub get_token_keys { my $self = shift; return keys %{$self->{token}}; }
    Take out the single quotes, especially around the things you need to evaluate (such as $_[0]). Perl used to require quotes on those keys which are barewords, but does not anymore.

    --
    [ e d @ h a l l e y . c c ]

Re: Hashes in Objects
by artist (Parson) on May 07, 2003 at 15:50 UTC
    Here is the sample code that can get you going.

    package Pen; use strict; use warnings; sub new { my $class = shift; my $self = { }; bless $self,$class; } sub set_token { my $self = shift; $self->{token}{$_[0]} = $_[1]; } sub get_token { my $self = shift; return $self->{token}{$_[0]}; } sub get_token_keys { my $self = shift; keys %{$self->{token}}; } package main; my $p = new Pen; $p->set_token( color => 'red'); $p->set_token( price => '10.0'); print $p->get_token( 'color'),"\n"; print join "\t",$p->get_token_keys(),"\n";
    artist
Re: Hashes in Objects
by hardburn (Abbot) on May 07, 2003 at 15:46 UTC

    You can do $self->{'tokens'} = { };, which makes the tokens key into an empty hashref.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://256255]
Approved by phydeauxarff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found