将散列引用作为参数从perl脚本传递给perl脚本 [英] Passing hash reference as an argument to perl script from perl script

查看:144
本文介绍了将散列引用作为参数从perl脚本传递给perl脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将散列引用作为参数从一个perl脚本(script1.pl)传递到另一个perl脚本(script2.pl)。这是我的代码的样子:



---------------------------- script1.pl ---------------------------------

 #!/ usr / bin / perl -w 

use strict;
使用警告;

my%hash =(
'a'=>Harsha,
'b'=>Manager
);

my $ ref = \%hash;

系统perl script2.pl $ ref;

--------------------- ------- script2.pl ---------------------------------

 #!/ usr / bin / perl -w 

use strict;
使用警告;

my%hash =%{$ ARGV [0]};

my $ string =a;

if(exists($ hash {$ string})){
print$ string = $ hash {$ string} \\\
;
}

这是输出错误:
$ (
sh:-c:line 0:`perl script2.pl HASH( 0x8fbed0)'

我找不到通过引用的正确方法。

解决方案

散列是一个内存数据结构,处理自己的内存空间,其他进程不能访问它。你认为它,我相信你会很快发现为什么。

哈希引用是该内存位置的地址,即使其他进程可能理解'它,它仍然无法访问内存空间。



这里我们讨论的实际上是一个相当大的概念 - 进程间通信或IPC - 非常详细,关于它的文档全称为 perlipc

长短小是这样 - 你不能做你想做的事。在进程之间共享内存比您想象的要困难得多。

您可以做的是来回传输数据 - 不是通过引用,而是包含实际的信息。



我建议对于你的例子,这个工作的工具是JSON,因为那样你就可以编码和解码你的哈希:

 #!/ usr / bin / perl -w 

use strict;
使用警告;

使用JSON;

my%hash =(
'a'=>Harsha,
'b'=>Manager
);

my $ json_string = to_json(\%hash);

print $ json_string;

这给出:

  {b:经理,a:Harsha} 

然后你可以在命令行上'传递'你的 $ json_string - 虽然要记住它中的任何空格会混淆 @ARGV 如果你不小心 - 或者通过 STDIN 有点儿。

然后在你的子过程中解码:

  use strict; 
使用警告;

使用JSON;

my $ json_string ='{b:经理,a:Harsha}';

my $ json = from_json($ json_string);

my $ string =a;

if(exists($ json - > {$ string})){
print$ string =,$ json - > {$ string}里, \\\
;

$ / code>

(您可以通过以下操作使其更类似于您的代码:

  my $ json = from_json($ json_string); 
my%hash =%$ json;

其他选项为:使用


  • 可存储 - 冻结和解冻(内存)或存储和检索(磁盘) 使用 IPC :: Open2 并发送数据到 STDIN



实际上有很多种选择 - 看看 perlipc 。但这不像'只是传递参考'那样简单。


I want to pass a hash reference as an argument from one perl script (script1.pl) to another perl script (script2.pl). This is how my code looks:

----------------------------script1.pl---------------------------------

#!/usr/bin/perl -w

use strict;
use warnings;

my %hash = (
'a'      => "Harsha",
'b'      => "Manager"
);

my $ref = \%hash;

system "perl script2.pl $ref";

----------------------------script2.pl---------------------------------

#!/usr/bin/perl -w

use strict;
use warnings;

my %hash = %{$ARGV[0]};

my $string = "a";

if (exists($hash{$string})){
    print "$string = $hash{$string}\n";
}

And this is the output error:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `perl script2.pl HASH(0x8fbed0)'

I can't figure out the right way to pass the reference.

解决方案

A hash is an in memory data structure. Processes 'own' their own memory space, and other processes can't just access it. If you think about it, I'm sure you'll spot why quite quickly.

A hash reference is an address of that memory location. Even if the other process could 'understand' it, it still wouldn't be able to access the memory space.

What we're talking about here is actually quite a big concept - Inter Process Communication or IPC - so much so there's a whole chapter of the documentation about it, called perlipc.

The long and short of it is this - you can't do what you're trying to do. Sharing memory between processes is much more difficult than you imagine.

What you can do is transfer the data back and forth - not by reference, but the actual information contained.

I would suggest that for your example, the tool for the job is JSON, because then you can encode and decode your hash:

#!/usr/bin/perl -w

use strict;
use warnings;

use JSON;

my %hash = (
    'a' => "Harsha",
    'b' => "Manager"
);

my $json_string = to_json( \%hash );

print $json_string;

This gives:

{"b":"Manager","a":"Harsha"}

Then your can 'pass' your $json_string - either on the command line, although bear in mind that any spaces in it confuses @ARGV a bit if you're not careful - or via STDIN.

And then decode in your sub process:

use strict;
use warnings;

use JSON;

my $json_string = '{"b":"Manager","a":"Harsha"}';

my $json = from_json ( $json_string );

my $string = "a";

if (exists($json -> {$string} )){
    print "$string = ",$json -> {$string},"\n";
}

(You can make it more similar to your code by doing:

my $json = from_json ( $json_string );
my %hash = %$json;

Other options would be:

  • use Storable - either freezing and thawing ( memory) or storing and retrieving (disk)
  • use IPC::Open2 and send data on STDIN.

There's a variety of options really - have a look at perlipc. But it's not as simple a matter as 'just passing a reference' unfortunately.

这篇关于将散列引用作为参数从perl脚本传递给perl脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆