导入.pl文件

Nin*_*ina 2 import perl

我想知道如何将Perl文件导入脚本。我尝试了使用,需求和操作,但似乎没有任何效果适合我。这就是我用require做到的:

#!/usr/bin/perl

require {
 (equations)
}

print "$x1\n";
Run Code Online (Sandbox Code Playgroud)

是否可以编写代码以将一个值(我在脚本中输入)替换为equations.pl,然后让我的脚本使用中定义的方程式equations.pl来计算另一个值?我该怎么做呢?

del*_*ver 5

您可以要求一个.pl文件,该文件随后将在其中执行代码,但是为了访问变量,您需要一个包,并且可以“使用”而不是require(简便方法)或通过Exporter。

http://perldoc.perl.org/perlmod.html

简单示例:这是您要导入的内容,将其命名为Example.pm:

package Example;

our $X = 666;

1;  # packages need to return true.
Run Code Online (Sandbox Code Playgroud)

以及使用方法:

#!/usr/bin/perl -w
use strict;

use Example;

print $Example::X;
Run Code Online (Sandbox Code Playgroud)

假定Example.pm位于同一目录中,或者位于@INC目录的顶层。