【发布时间】:2015-07-17 09:34:29
【问题描述】:
我想将哈希引用作为参数从一个 perl 脚本 (script1.pl) 传递给另一个 perl 脚本 (script2.pl)。这就是我的代码的样子:
----------------------------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";
}
这是输出错误:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `perl script2.pl HASH(0x8fbed0)'
我想不出传递引用的正确方法。
【问题讨论】:
-
在 CPAN 上检查
JSON或Storable和MIME::Base64。 -
使用
use warnings时,不需要在shebang行中使用-w。 -
不要使用
system,而是使用require。