Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-09-22 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (190 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!