Your problem is that all of your data has been stored as
globals in Class2. What you need to do is store it all
within your object, and then you can have multiple objects
in your class without problems. Here is the code. I have
made the following changes:
- I changed the formatting. Hanging indent and 2 space
indent is my habit. Hanging vs not hanging doesn't
really matter. However there is solid evidence that
having an indent in the range 2-4 significantly improves
comprehension.
- I added strict. This will catch many unintentional
mistakes. Look at its documentation for more. And if you
do need globals, see vars.
- I sprinkled my liberally through the module.
- I moved data into the object.
- I changed your code to use the two argument form of
bless.
Here it is:
package Class2;
use strict;
sub new {
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub data {
@_ == 3 or die 'usage: Class2->data( DATE, SUBJ )';
my $me = shift;
$me->{date} = shift;
$me->{subj} = shift;
print "The Date is: $me->{date}\n";
print "The Subject is: $me->{subj}\n";
}
sub printClass {
my $self = shift;
my $class = ref($self);
print "Your class type is $class\n";
print "The Date is: $self->{date}\n";
print "The Subject is: $self->{subj}\n";
}
sub getDate {
(shift)->{date};
}