1

地址.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?

4

1 回答 1

8

该模块应存储在Address.pm(pm (for Perl Module ) 而不是 pl) 中,并且您应该正确拼写package

另请参阅perldoc perlmodPerl 模块的示例。

于 2013-05-10T16:22:06.660 回答