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());
}
}
人在这里定义:
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;
这将导致
错误:无法在 check2.pl 第 22 行对未定义值调用方法“getFirstName”。
两种情况下不会出现该错误:
将变量声明为
our
.
例子:our object;
重命名函数名称之一:
allthis
到allthis1
任何人都可以解释原因吗?