[英]perl command line backticks with matrix
我想通過使用帶有反引號的命令行將矩陣從一個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之后,終端輸出:
我也嘗試過在other_file.pl中成功使用符號引用 (輸出為:“ other_file.pl中沒有數組引用”)
任何想法 ? 非常感謝。
PS:在命令行中使用簡單變量$ var沒問題;
好的,從根本上講-不可能直接做您想做的事情。 數組是一種內存狀態,它無法方便地打包傳遞。 這就是為什么您得到類似ARRAY(0x6c0cb8)
之類的原因-這是因為它是數組(或子元素)占用的內存地址。
因此,無論您如何執行此操作,都需要先渲染數組,然后在子程序中對其進行解析。
這是一個足夠大的主題,整個文檔中都有有關這一部分的內容: perlipc
采取的方法因您要完成的目標而異。 對於您要嘗試做的事情,我的第一個想法就是看Storable
:
use warnings;
use strict;
use Storable;
my @matrix = ( [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] );
my $tempfile = "matrix.$$";
store \@matrix, $tempfile;
my $results = `perl other_file.pl $tempfile`; #option 1
print $results;
並在“ other_file”中使用retrieve
:
use warnings;
use strict;
use Data::Dumper;
use Storable qw/retrieve/;
my ($filename) = @ARGV;
print Dumper retrieve($filename);
(盡管您可能應該使用File::Temp
而不是臨時文件的任意名稱)。
但是,請看一下perlipc
,它有很多關於如何來回傳遞信息的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.