一个文件下有两个文件,pm和pl。如果你运行pl文件,pm包调用不成功
There are two files, pm and pl under one file. If you run the pl file, the pm package call is unsuccessful
文件目录:
/home/wh/perlstudy/perl2/Person/Student.pm
/home/wh/perlstudy/perl2/Person/person.pl
Student.pm
package Student;
use strict;
use warnings FATAL => 'all';
# use utf8;
# binmode(STDIN,"encoding(gbk)");
sub new
{
my $class = shift;
my $self = {
_name => shift, _rank => shift, };
# Print all the values just for clarification.
print "获取学生名字 $self->{_name}\n";
print "获取学生排名 $self->{_rank}\n";
bless $self, $class;
return $self;
}
sub studentRank {
my ( $self, $name ) = @_;
$self->{_name} = $name if defined($name);
return $self->{_name};
}
sub studentName {
my( $self ) = @_;
return $self->{_name};
}
1;
person.pl
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
# use utf8;
# binmode(STDOUT,"encoding(gbk)");
BEGIN(push @INC,"/home/wh/perlstudy/perl2/Person/");
use Student;
my$object = Student->new( "Ana", "9th");
# name which is set using constructor.
my$name = $object->studentName();
print "Name set using constructor is : $name\n";
# name set using helper function.
$object->studentRank( "Anastasia" );
# getting name set by helper function.
$name = $object->studentName();
print "名字 set using helper is : $name\n";
我得到:
Prototype after '@' for BEGIN : push @INC,"/home/wh/perlstudy/perl2/Person/" at perlson.pl line 6.
想解决@INC
以外的perl模块的使用问题。
BEGIN是一个代码块,所以需要花括号:
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
# use utf8;
# binmode(STDOUT,"encoding(gbk)");
BEGIN {
push @INC, "/home/wh/perlstudy/perl2/Person/";
}
# rest of person.pl goes here
或者您在调用 Perl 时使用 -I
标志:
perl -I/home/wh/perlstudy/perl2/Person/ person.pl
这样做的好处是您不必对路径进行硬编码,但每次都必须重新键入。 (或者为它创建别名或 shell 脚本。)
或use lib:
use lib "/home/wh/perlstudy/perl2/Person/";
最后,您可以将 Perl 模块安装在 Perl 默认查找模块的位置,但在开发过程中,每次更改 Student.pm
.
时都必须这样做
文件目录:
/home/wh/perlstudy/perl2/Person/Student.pm
/home/wh/perlstudy/perl2/Person/person.pl
Student.pm
package Student;
use strict;
use warnings FATAL => 'all';
# use utf8;
# binmode(STDIN,"encoding(gbk)");
sub new
{
my $class = shift;
my $self = {
_name => shift, _rank => shift, };
# Print all the values just for clarification.
print "获取学生名字 $self->{_name}\n";
print "获取学生排名 $self->{_rank}\n";
bless $self, $class;
return $self;
}
sub studentRank {
my ( $self, $name ) = @_;
$self->{_name} = $name if defined($name);
return $self->{_name};
}
sub studentName {
my( $self ) = @_;
return $self->{_name};
}
1;
person.pl
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
# use utf8;
# binmode(STDOUT,"encoding(gbk)");
BEGIN(push @INC,"/home/wh/perlstudy/perl2/Person/");
use Student;
my$object = Student->new( "Ana", "9th");
# name which is set using constructor.
my$name = $object->studentName();
print "Name set using constructor is : $name\n";
# name set using helper function.
$object->studentRank( "Anastasia" );
# getting name set by helper function.
$name = $object->studentName();
print "名字 set using helper is : $name\n";
我得到:
Prototype after '@' for BEGIN : push @INC,"/home/wh/perlstudy/perl2/Person/" at perlson.pl line 6.
想解决@INC
以外的perl模块的使用问题。
BEGIN是一个代码块,所以需要花括号:
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
# use utf8;
# binmode(STDOUT,"encoding(gbk)");
BEGIN {
push @INC, "/home/wh/perlstudy/perl2/Person/";
}
# rest of person.pl goes here
或者您在调用 Perl 时使用 -I
标志:
perl -I/home/wh/perlstudy/perl2/Person/ person.pl
这样做的好处是您不必对路径进行硬编码,但每次都必须重新键入。 (或者为它创建别名或 shell 脚本。)
或use lib:
use lib "/home/wh/perlstudy/perl2/Person/";
最后,您可以将 Perl 模块安装在 Perl 默认查找模块的位置,但在开发过程中,每次更改 Student.pm
.