Perl的“使用"语法如何工作? [英] How does the Perl 'use' syntax work?

查看:59
本文介绍了Perl的“使用"语法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

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 by sort.

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆