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
line in every member function gives you a blessed reference to the hash.my $self = shift;
So instead of writing
you should write$self{theValue}
$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 '->' }
|
---|
In Section
Seekers of Perl Wisdom