I need help, please, in how to make an instance variable in a class (instead of a class variable). Have searched using google and ask.com, looked through Conway's OO Perl book online...but haven't come up with any solutions.
Here's some sample code that demonstrates the problem. First, the Test package.
package Test;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
sub getValue {
my $self = shift;
$self{theValue};
}
sub setValue {
my $self = shift;
my $value = shift;
$self{theValue} = $value;
}
Second, the test code that references the Test package.
use Test;
my $test = new Test;
print "test is " .$test ."\n";
print "test getValue after new is " .$test->getValue() ."\n";
$test->setValue(1);
print "test getValue after setValue(1) is ". $test->getValue() ."\n";
print "\n";
my $test1 = new Test;
print "test1 is " .$test1 ."\n";
print "test1 getValue after new is " .$test1->getValue() ."\n";
#print "test getValue after test1->new is ". $test->getValue() ."\n";
$test1->setValue(2);
print "test1 getValue after setValue(2) is " .$test1->getValue() ."\n"
+;
print "test getValue after test1->setValue(2) is ". $test->getValue()
+."\n";
Here is the output with some comments added.
test is Test=HASH(0x88fbc20)
Use of uninitialized value in concatenation (.) or string at test.pl l
+ine 9. #expected
test getValue after new is
test getValue after setValue(1) is 1
test1 is Test=HASH(0x88fbde8) #different than test
test1 getValue after new is 1 #wow, test1 "inherited" the value set by
+ the test object
test1 getValue after setValue(2) is 2
test getValue after test1->setValue(2) is 2 #and now test sees 2, inst
+ead of 1
I am surprised that test1 "steps on" test because the hash keys are different for test and test1. $test->setValue(1) sets the value to 1 for the test object, but after calling $test
1->setValue(2), both test and test1 report 2 when getValue is called.
So it seems I have a class variable here, one that holds the same value for all instances of the class. How do I get an instance variable, where each class instance maintains its own distinct value?
Many thanks for your help. Regards, Rich
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.