【发布时间】:2013-05-10 16:17:17
【问题描述】:
地址.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 中的使用地址行找不到地址 这两个 perl 文件在同一个文件夹中。
test.pl 需要做什么才能看到 Address.pl?
【问题讨论】:
-
不要写没有
use strict; use warnings;的Perl。 -
我以前发过这个,它的语气不是最好的所以我不再发了,但是我现在没有时间输入更好的东西:joelslinux.blogspot.com/2011/06/use-strict-and-warnings.html :-)跨度>
-
@ealeon:使用它们的目的是阻止你做愚蠢的事情。但是可以免费阅读完整的文档 - perldoc.perl.org/strict.html perldoc.perl.org/warnings.html
标签: perl