Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Perl class - how to create instance variable instead of class variable

by lyklev (Pilgrim)
on Aug 29, 2007 at 20:30 UTC ( #635930=note: print w/replies, xml ) Need Help??


in reply to Perl class - how to create instance variable instead of class variable

You are almost there. What you have to keep in mind is that the
my $self = shift;
line in every member function gives you a blessed reference to the hash.

So instead of writing

$self{theValue}
you should write
$self -> {theValue}

Your modified code in full:

package Test; sub new { my $class = shift; my $self = {}; bless $self, $class; return $self; } sub getValue { my $self = shift; $self -> {theValue}; # note '->' } sub setValue { my $self = shift; my $value = shift; $self -> {theValue} = $value; # note '->' }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://635930]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2018-01-20 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (227 votes). Check out past polls.

    Notices?