【问题标题】:Perl use function visibilityPerl 使用函数可见性
【发布时间】: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?

【问题讨论】:

标签: perl


【解决方案1】:

模块应该存储在Address.pm(pm(对于Perl Module)而不是pl)并且你应该正确拼写package

有关 Perl 模块的示例,另请参阅 perldoc perlmod

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 2014-06-23
    • 2020-09-18
    • 2020-01-04
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多