问题描述
我需要一些帮助来读取在perl的主脚本中执行的子脚本中分配的值
I need some help with reading value assigned inside sub scripts executed within main script in perl
- 我有"main.pl",在其中声明和分配了很少的哈希值(
%var1
%var2
),并且正在处理整个脚本中的值. - 在main.pl中,我使用
system(perl sub_script.pl)
执行perl脚本"sub_scirpt.pl" - 在sub.pl内部,如果我为
%var1
%var2
分配的值很少 - 如何在main.pl脚本级别打印
%var1
的sub_script.pl中分配的值?
- I have "main.pl" where i have few hashes (
%var1
%var2
) declared and assigned and am processing the values throughout the script. - Within main.pl, i execute a perl script "sub_scirpt.pl" using
system(perl sub_script.pl)
- Inside sub.pl, if i am assigning few value to
%var1
%var2
- How do i print the values assigned in sub_script.pl of
%var1
at main.pl script level?
希望上面的描述对我的要求有所帮助.
Hope the above description gives some picture on my requirement.
sub_script.pl的内容
content of sub_script.pl
our %var1 ;
our $var1 = "start";
our $var2 = "abcd";
our $var3 = "efgh";
our @var4 = qw{test1 test2 test3};
$var1{$var2}{$var3}->{test1} = "1234";
$var1{$var2}{$var3}->{test2} = "5679";
$var1{$var2}{$var3}->{test3} = "5555";
foreach $var (@var4) {
print "sub_script:: var1: $var1 \nsub_script:: var2: $var2 \nsub_script:: var3: $var3 \nsub_script:: var4: $var \nsub_script:: hash-value: $var1{$var2}{$var3}->{$var}\n";
}
sub_script.pl的输出:
Output of sub_script.pl:
sub_script:: var1: start
sub_script:: var2: abcd
sub_script:: var3: efgh
sub_script:: var4: test1
sub_script:: hash-value: 1234
sub_script:: var1: start
sub_script:: var2: abcd
sub_script:: var3: efgh
sub_script:: var4: test2
sub_script:: hash-value: 5679
sub_script:: var1: start
sub_script:: var2: abcd
sub_script:: var3: efgh
sub_script:: var4: test3
sub_script:: hash-value: 5555
main.pl的内容
content of main.pl
@var1 = qw{start end};
$var1 = @var1[1];
our $var2 = "abcd";
our $var3 = "efgh";
@var4 = qw{test1 test2 test3};
system ("perl sub_script.pl");
print "\n\n";
foreach $var (@var4) {
print "main:: var1: $var1 \nmain:: var2: $var2 \nmain:: var3: $var3 \nmain:: var4: $var \nmain::hash-value:$var1{$var2}{$var3}->{$var}\n";
}
main.pl的输出:
Output of main.pl:
main:: var1: end
main:: var2: abcd
main:: var3: efgh
main:: var4: test1
main:: hash-value:
main:: var1: end
main:: var2: abcd
main:: var3: efgh
main:: var4: test2
main:: hash-value:
main:: var1: end
main:: var2: abcd
main:: var3: efgh
main:: var4: test3
main:: hash-value:
预期输出:
main:: var1: start
main:: var2: abcd
main:: var3: efgh
main:: var4: test1
main:: hash-value: 1234
main:: var1: start
main:: var2: abcd
main:: var3: efgh
main:: var4: test2
main:: hash-value: 5679
main:: var1: start
main:: var2: abcd
main:: var3: efgh
main:: var4: test3
main:: hash-value: 5555
-预先感谢
推荐答案
简而言之,您需要将数据从程序传递回调用它的程序.
In short, you need to pass data from a program back to the program that invoked it.
如果被调用程序(sub_script.pl
)仅需要返回简单值,则可以将它们打印到STDOUT
,在此,调用程序(main.pl
)通过反引号"( qx (运算符形式),它返回程序的STDOUT
.
If the called program (sub_script.pl
) needs to return only simple values then it can just print them to STDOUT
, where the calling program (main.pl
) runs it via "backticks" (qx in the operator form), which returns the program's STDOUT
.
但是,需要更复杂的数据结构序列化,以便可以有意义地向下发送它们某种字节的管道(某种形式).好的库有多种实现方法,下面的简单代码演示了其中的两种.
However, more complex data structures need be serialized so that they can be meaningfully sent down a pipe (of some kind) byte by byte. There are various ways to do so, with good libraries, and simple code below demonstrates two of them.
JSON格式非常简单,易于阅读,可以从任何语言访问和系统.在Perl数据结构和相应的JSON之间进行转换的Perl库非常易于使用.这是适用于多种情况的良好且安全的解决方案.
The JSON format is very simple, human readable, and accessible from any language and system. Perl libraries for conversion between Perl data structures and corresponding JSON are very simple to use. This is a good and safe solution for a wide range of situations.
Perl的可存储是一种持久性工具,因此可以将复杂的数据结构转储到磁盘,然后从文件中还原.还可以将数据序列化为可以在程序之间传递的字符串,而无需文件.
Perl's Storable is a tool for persistency, so that complex data structures can be dumped to disk and later restored from the file. One can also serialize data to strings that can be passed between programs with no need for files.
下面的程序 sub_script.pl 创建一个哈希,并从中生成一个JSON
字符串并进行打印,然后还将哈希序列化为一个Storable
字符串,并同时打印
The program sub_script.pl below creates a hash and makes a JSON
string out of it and prints it, and then also serializes the hash into a Storable
string which it also prints
use warnings;
use strict;
use feature 'say';
use JSON;
use Storable qw(nfreeze);
my %h = (a => { A => 1}, b => { B => 2 });
my $str_json = encode_json \%h;
say $str_json;
my $str_st = nfreeze \%h;
say $str_st;
以下程序( main.pl )使用qx
运行上面的sub_script.pl
,以便它接收其STDOUT
,然后从JSON
和Storable
字符串.在这两种情况下,我们都返回一个hashref,即对原始哈希之类的数据的引用
The following program (main.pl) runs the sub_script.pl
above using qx
so that it receives its STDOUT
, and then it rebuilds the hash from both the JSON
and Storable
strings. In both cases we get back a hashref, a reference to data like the original hash
use warnings;
use strict;
use feature 'say';
use JSON;
use Storable qw(thaw);
use Data::Dump qw(dd);
my @ret = qx( sub_script.pl );
chomp @ret;
my $hr1 = decode_json $ret[0];
dd $hr1;
my $hr2 = thaw $ret[1];
dd $hr2;
为简单起见,我使用 JSON ,如果已安装,它将使用快速的JSON_XS
,或者纯Perl后备,核心JSON_PP
.另一个选项是 Cpanel :: JSON :: XS ,它是.
I use JSON for simplicity, which will use the fast JSON_XS
if installed, or the pure-Perl fallback, the core JSON_PP
. Another option is Cpanel::JSON::XS, a fork of JSON_XS
.
要打印复杂的数据结构以便轻松查看它们,我使用 Data :: Dump ,可能需要安装.还有许多其他好的模块可以显示复杂的数据,其中使用最广泛的模块是核心数据:: Dumper (因此无需安装).
To print complex data structures so to see them easily I use Data::Dump, which probably need be installed. There is a number of other good modules to show complex data, the most widely used one being the core Data::Dumper (so no need to install).
在所有这些情况下,被调用的程序仍会打印到其STDOUT
.当数据交换需求变得更加复杂时,您可能需要使用一些进程间通信"( IPC )技术.
In all of this, the program that is called still just prints to its STDOUT
. When data exchange needs get more complex you may need to use some "Inter-Process-Communication" (IPC) techniques.
这篇关于如何在运行脚本的程序中读取在脚本内部分配的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!