Address.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",
);test.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行找不到Address,两个perl文件在同一文件夹中。
我必须做什么才能让test.pl看到Address.pl?
发布于 2013-05-11 00:22:07
该模块应该存储在Address.pm中(pm (对于Perl Module),而不是pl),并且您应该正确地拼写package。
有关perldoc perlmod模块的示例,请参阅Perl。
https://stackoverflow.com/questions/16486587
复制相似问题