Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by lyklev (Pilgrim)
on Aug 29, 2007 at 20:30 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://635930]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found