use Person;
print(" object creation ");
my $object = new Person( "Mohammad", "Saleem", 23234345);
print (" here after");
allthis();
sub allthis()
{
print( $object->getFirstName() );
}
if(1)
{
print(" object creation ");
my $object = new Person( "Mohammad", "Saleem", 23234345);
allthis();
sub allthis()
{
print($object->getFirstName());
}
}
Person is defined here:
package Person;
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;
This leads to
Error : Can't call method "getFirstName" on an undefined value at check2.pl line 22.
The error won't occur in two cases:
declare the variables as
our
.
example:our object;
rename one of the function names:
allthis
toallthis1
Can any one explain the reason?