【发布时间】:2014-11-22 22:44:04
【问题描述】:
我想使用带反引号的命令行将 矩阵 从一个 perl 文件传输到另一个文件。
在 perl 的第一个文件 source.pl 上:
use warnings;
use strict;
my @matrix = (
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
);
my $results = `perl other_file.pl @matrix`; #option 1
# my $results = `perl other_file.pl '@matrix'`; #option 2
print $results;
在 other_file.pl 上
use strict
use warnings
my @matrix_other = $ARGV[0];
print "mat_adress = ".$matrix_other[1][2]."\n";
启动source.pl后,终端输出:
- 使用选项 1:sh:1:语法错误:“(”意外
- 使用选项 2:不能使用字符串 ("ARRAY(0x6c0cb8) ARRAY(0x6df410) "...) 作为 ARRAY 引用,而在 other_file.pl 第 5 行使用“严格引用”。
我也尝试在 other_file.pl 中使用 Symbolic references 但没有成功(输出为:“Not an ARRAY reference at other_file.pl”)
有什么想法吗? 非常感谢。
PS:命令行中简单的变量$var没问题;
【问题讨论】:
-
您不能将二维数组插入到这样的字符串中。
ARRAY(0x6c0cb8)是已字符串化的数组引用。此外,您不能像这样通过 shell 将一个变量传输到另一个 Perl 程序——如果您想要这样,您需要使用 Perl 连接这些程序。 -
您需要以一种或另一种方式呈现然后解析以在命令行上传输。像
Storable这样的东西可能会奏效吗?
标签: perl matrix command line backticks