I learn perl, and when I tried to do object orientation, I ran into errors, This is the code, Test.pm
package Test;
sub new
{
my $class = shift;
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
print "First Name is $self->{_firstName}\n";
print "Last Name is $self->{_lastName}\n";
print "SSN is $self->{_ssn}\n";
bless $self, $class;
return $self;
}
sub setFirstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}
sub getFirstName {
my( $self ) = @_;
return $self->{_firstName};
}
1;
and test.pl
use Test;
$object = Test::new( "Mohammad", "Saleem", 23234345);
$firstName = $object->getFirstName();
print "Before Setting First Name is : $firstName\n";
$object->setFirstName( "Mohd." );
$firstName = $object->getFirstName();
print "Before Setting First Name is : $firstName\n";
and when I try to run, it shows some erroe like this,
Can't locate object method "new" via package "Test" at test.pl line 2.
What is the mistake in this object oriented program?
source
share