我不知道我的代码有什么问题。我正在尝试序列化父级内部的哈希并将其通过管道传输到应该反序列化的叉子。
#!/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
不会反序列化检索到的标量值。也许检索到的标量值不正确。
我试图在没有分叉或管道的情况下做一些半成品,并且它的工作原理:
#!/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";
没有太大的逻辑差异,他?如果有人能向我解释更多这种行为,那就太好了。