2013-05-10 51 views
1

Address.plPerl的使用功能知名度

#!/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?

+9

不要寫'沒有'use strict;使用警告;'。 – Quentin 2013-05-10 16:22:49

+4

我曾經發布這個,它的語氣不是最好的,所以我現在不再了,但我現在沒有時間鍵入更好的東西:http://joelslinux.blogspot.com/2011/06/use -strict-and-warnings.html :-) – 2013-05-10 16:39:11

+1

@ealeon:使用它們的目的是阻止你做愚蠢的事情。但免費閱讀完整的文檔 - http://perldoc.perl.org/strict.html http://perldoc.perl.org/warnings.html – 2013-05-12 12:44:19

回答

8

該模塊應該存儲在Address.pm(下午(對於Perl模塊)而不是pl),您應該正確拼寫package。請參閱perldoc perlmod瞭解Perl模塊的示例。