我不知道什么是错我的代码。我试图序列化一个哈希内部的散列,并将其传递给叉,它应该被反序列化。的Perl:管一个序列化的哈希分叉过程
#!/usr/bin/perl
use strict;
use warnings;
use Storable qw(freeze thaw);
use IO::Pipe;
my $pipe_to_fork = IO::Pipe->new();
my $fork = fork;
if ($fork == 0) { # actual fork scope
$pipe_to_fork->reader();
my $hash_serialized = <$pipe_to_fork>; # wait and retrieve the serialized hash from parent
chomp $hash_serialized;
my %hash_rebuild = %{thaw($hash_serialized)}; # deserialize the retrieved serialized hash
exit;
}
my %hash = ('key1' => "val1", 'key2' => "val2");
$pipe_to_fork->writer();
$pipe_to_fork->autoflush(1);
my $hash_serialized = freeze(\%hash); # serialize the hash
print $pipe_to_fork $hash_serialized."\n";
sleep 5;
exit;
...产生以下错误:
Can't use an undefined value as a HASH reference at ./fork_serialize.pl line 14, <GEN0> line 1.
是不是有什么毛病管?看来thaw
不会反序列化检索到的标量值。也许检索到的标量值不正确。
我一直试图做一些事情semilar不分叉或管道,其工作:
#!/usr/bin/perl
use strict;
use warnings;
use Storable qw(freeze thaw);
my %hash = ('key1' => "value1", 'key2' => "value2");
my $hash_serialized = freeze(\%hash);
my %hash_rebuild = %{thaw($hash_serialized)};
print $hash_rebuild{'key2'}."\n";
没有太大的差异逻辑的,他?如果有人能更多地解释我的这种行为,这将是很好的。
检查发送和接收的字节数是相同的。 – tlrrd