地址.pl
#!/usr/bin/perl
pacakge Address;
sub new {
my $package = shift;
my $self = {@_};
return bless $self, $package;
}
sub country {
my $self = shift;
return @_ ? ($self->{country} = shift) : $self->{country};
}
sub as_string {
my $self = shift;
my $string;
foreach (qw(name street city zone country)) {
$string .= "$self->{$_}\n" if defined $self->{$_};
}
return $string;
}
$test = Address-> new (
name => "Sam Gamgee",
street => "Bagshot Row",
city => "Hobbiton",
country => "The Shire",
);
测试.pl
use Address;
$test = Address-> new (
name => "Sam Gamgee",
street => "Bagshot Row",
city => "Hobbiton",
country => "The Shire",
);
print $test->as_string;
在 test.pl 中的 use Address 行找不到地址,这两个 perl 文件位于同一文件夹中。
我必须为 test.pl 做什么才能看到 Address.pl?