ealeon
ealeon

Reputation: 12462

Perl use function visibility

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;

It cannot find Address at the line use Address in test.pl the two perl files are in the same folder.

What do I have to do for test.pl to see Address.pl?

Upvotes: 1

Views: 104

Answers (1)

Quentin
Quentin

Reputation: 944202

The module should be stored in Address.pm (pm (for Perl Module) not pl) and you should spell package correctly.

See also perldoc perlmod for an example of a Perl module.

Upvotes: 8

Related Questions