本文介绍了导入.pl文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering how to import a Perl file to a script. I experimented with use, require and do, but nothing seems to work for me. This is how I did it with require:

#!/usr/bin/perl

require {
 (equations)
}

print "$x1\n";

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

Is it possible to code for substituting a value (I get in my script) into equations.pl, then have my script use an equation defined in equations.pl to calculate another value? How do I do this?

推荐答案

您可以要求一个.pl文件,该文件随后将在其中执行代码.但是,要访问变量,您需要一个程序包,并且要么"(而不是require(简单方法)或通过Exporter.

You can require a .pl file, which will then execute the code in it, but in order to access variables, you need a package, and either "use" instead of require (the easy way) or via Exporter.

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

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

Simple example: here's the stuff you want to import, name it Example.pm:

package Example;

our $X = 666;

1;  # packages need to return true.

这是使用方法:

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

use Example;

print $Example::X;

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

This presumes Example.pm is in the same directory, or the top level of an @INC directory.

这篇关于导入.pl文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:01