1

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;

2 つの perl ファイルが同じフォルダーにある test.pl の use Address 行で Address が見つかりません。

test.pl が Address.pl を表示するにはどうすればよいですか?

4

1 に答える 1

8

Address.pmモジュールは (pl ではなくpm ( Perl Moduleの場合))に格納する必要があり、package正しいスペルを入力する必要があります。

perldoc perlmodPerl モジュールの例については、も参照してください。

于 2013-05-10T16:22:06.660 に答える