如何调用另一个perl文件中声明的变量?

时间:2015-09-21 03:18:47

标签: windows perl

script1.pl script2.pl 。我正在寻找使 script2.pl 能够从 script1.pl 调用 <?php $this->breadcrumbs = array( 'Divisions' => array('index'), 'Manage', ); $this->menu = array( array('label' => 'List Divisions', 'url' => array('index')), array('label' => 'Create Divisions', 'url' => array('create')), ); "); ?> <div class="row"> <?php $this->renderPartial('_dropdownfilter', array( 'model' => $model, )); ?> </div><!-- end dropdown partial form --> <?php $this->widget('zii.widgets.grid.CGridView', array( 'id' => 'divisions-grid', 'dataProvider' => $model->search(), 'filter' => $model, 'columns' => array( 'CompanyID', 'DivisionID', 'Name', array( 'class' => 'CButtonColumn', ), ), )); ?> 的值。

script1.pl

$string

script2.pl

$string="word1 word2 word3 word4 word5 word6 word7 word8 word9";
$cmd="perl \"My\\File\\Path\\script2.pl\"";
system ($cmd);

注意:我在Windows上使用perl。

2 个答案:

答案 0 :(得分:0)

最佳做法是使用模块。请参阅perlmod

在您的情况下,您可以使用require。通过添加require确保1个文件返回真值。

script1.pl:

#!/usr/bin/perl
use warnings;
use strict;

our $string = "word1 word2 word3 word4 word5 word6 word7 word8 word9";
our $cmd = "perl \"My\\File\\Path\\script2.pl\"";
system ($cmd);

1;

script2.pl:

#!/usr/bin/perl
use strict;
use warnings;    
use vars qw($string);

require "script1.pl";

print $string, "\n";

输出:

word1 word2 word3 word4 word5 word6 word7 word8 word9

答案 1 :(得分:0)

虽然你可以做到这一点,但你最好把变量作为命令行参数,或者如果有很多数据传递给STDIN。

# script1.pl
my $cmd = qq[$^X "My\\File\\Path\\script2.pl"];
my @words = qw[word1 word2 word3 word4 word5 word6 word7 word8 word9];
system $cmd, @words;

# script2.pl
print join ", ", @ARGV;

这不能很好地扩展。你最好将script2.pl重写为库并调用函数。

# mylibrary.pl
sub print_stuff {
    print join ", ", @_;
}

# script1.pl
require 'mylibrary.pl';

print_stuff(qw[word1 word2 word3 word4 word5 word6 word7 word8 word9]);

对于一些功能,这将工作正常。最后,您需要查看writing modules