从另一个调用一个Perl程序

Pyj*_*ava 0 perl

我有两个Perl文件,我想用另一个参数调用另一个文件

第一个文件 a.pl

$OUTFILE  = "C://programs/perls/$ARGV[0]";
# this should be some out file created inside work like C://programs/perls/abc.log
Run Code Online (Sandbox Code Playgroud)

第二档 abc.pl

require "a.pl" "abc.log";

# $OUTFILE is a variable inside a.pl and want to append current file's name as log.
Run Code Online (Sandbox Code Playgroud)

我希望它创建一个名为log的输出文件,作为当前文件的名称.

一个约束了我就是用$OUTFILE在这两个a.plabc.pl.

如果有更好的方法,请建议.

sim*_*que 7

require关键字只需要一个参数.这可以是文件名或包名.你的路线

require "a.pl" "abc.log";
Run Code Online (Sandbox Code Playgroud)

是错的.它在运算符期望字符串行中给出语法错误.

您可以.pl从另一个文件中获取一个文件.pl,但这是一个非常老式的,写得很糟糕的Perl代码.

如果两个文件都没有定义包,则代码隐式放在main包中.您可以在外部文件中声明包变量,并在需要的文件中使用它.

abc.pl:

use strict;
use warnings;

# declare a package variable
our $OUTFILE  = "C://programs/perls/filename";

# load and execute the other program
require 'a.pl';
Run Code Online (Sandbox Code Playgroud)

并在a.pl:

use strict;
use warnings;

# do something with $OUTFILE, like use it to open a file handle
print $OUTFILE;
Run Code Online (Sandbox Code Playgroud)

如果你运行它,它将打印出来

C://programs/perls/filename
Run Code Online (Sandbox Code Playgroud)