Perl的“使用"语法如何工作? [英] How does the Perl 'use' syntax work?
问题描述
示例代码:
m1.pm
my $a;
my $b;
sub init {
$a = shift;
$b = shift;
}
sub printab {
print "a = -$a-\n";
print "b = -$b-\n";
}
1;
m2.pm
my $a;
my $b;
sub init {
$a = shift;
$b = shift;
}
1;
test.pl
use strict;
use warnings;
use m1;
use m2;
init('hello', 'world');
printab();
运行:
$ perl test.pl
a = --
b = --
$
发生的事情是init('hello', 'world')
调用被映射到m2.pm
并在那里初始化变量($a
和$b
).
这种方式很有意义,但是我不明白的是为什么这些值在test.pl
中不可用.
-
我在这里尝试做一些根本错误的事情吗? 使用具有相同命名子例程和变量的两个模块的正确方法是什么?
-
Perl
use
的工作原理是什么?如果有人可以将其与C的#include
指令进行对比,将会有所帮助.
首先,请务必阅读 perldoc perlmod .>
您都不在任何一个模块中声明名称空间,因此所有内容都在main
名称空间中.在m1.pm
中声明package m1;
,在m2.pm
中声明package m2;
.
至少,您应该实现import
方法(或继承Exporter
提供的方法),以便使用模块的程序可以决定从何处导入内容.
在我看来,您也在探索OO的边缘.
进一步:
-
避免使用
$a
和$b
作为变量名,因为很容易将它们与sort
使用的包变量$a
和$b
混淆. -
请勿使用小写的模块名称:它们保留给实用程序.
一个最小的实现(为了方便测试,全部放在一个文件中)如下所示:
package My::M1;
use strict; use warnings;
sub new { my $class = shift; bless { @_ } => $class }
sub a {
my $self = shift;
my ($v) = @_;
$self->{a} = $v if @_;
return $self->{a};
}
sub b {
my $self = shift;
my ($v) = @_;
$self->{b} = $v if @_;
return $self->{b};
}
package My::M2;
use strict; use warnings;
use base 'My::M1';
sub printtab {
my $self = shift;
for my $x (qw(a b)) {
printf "%s = -%s-\n", $x, $self->$x;
}
}
package main;
my $m = My::M2->new(a => 'hello', 'b' => 'world');
$m->printtab;
Sample code:
m1.pm
my $a;
my $b;
sub init {
$a = shift;
$b = shift;
}
sub printab {
print "a = -$a-\n";
print "b = -$b-\n";
}
1;
m2.pm
my $a;
my $b;
sub init {
$a = shift;
$b = shift;
}
1;
test.pl
use strict;
use warnings;
use m1;
use m2;
init('hello', 'world');
printab();
Run:
$ perl test.pl
a = --
b = --
$
What happens is that the init('hello', 'world')
call is mapped to m2.pm
and initializes the variables ($a
and $b
) there.
This kind of makes sense, but what I do not understand is why those values are not available in test.pl
.
Is there something fundamentally wrong that I am trying to do here? What is the correct way to use two modules with same named subroutines and variables?
How exactly does a Perl
use
work? It would help if someone could contrast it with C's#include
directive.
First, do read perldoc perlmod.
You do not declare a namespace in either module, so everything is in the main
namespace. Declare package m1;
in m1.pm
and package m2;
in m2.pm
.
At the very least, you should implement an import
method (or inherit the one Exporter
provides) so that programs that use modules can decide what to import from where.
It also seems to me that you are exploring around the edges of OO.
Further:
Avoid using
$a
and$b
as variable names because it is easy to confuse them with the package variables$a
and$b
used bysort
.Don't use lower case module names: They are reserved for pragmata.
A minimal implementation (all in one file for testing convenience) looks like this:
package My::M1;
use strict; use warnings;
sub new { my $class = shift; bless { @_ } => $class }
sub a {
my $self = shift;
my ($v) = @_;
$self->{a} = $v if @_;
return $self->{a};
}
sub b {
my $self = shift;
my ($v) = @_;
$self->{b} = $v if @_;
return $self->{b};
}
package My::M2;
use strict; use warnings;
use base 'My::M1';
sub printtab {
my $self = shift;
for my $x (qw(a b)) {
printf "%s = -%s-\n", $x, $self->$x;
}
}
package main;
my $m = My::M2->new(a => 'hello', 'b' => 'world');
$m->printtab;
这篇关于Perl的“使用"语法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!