2

我不知道我的代码有什么问题。我正在尝试序列化父级内部的哈希并将其通过管道传输到应该反序列化的叉子。

#!/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";

没有太大的逻辑差异,他?如果有人能向我解释更多这种行为,那就太好了。

4

2 回答 2

6

问题是您正在尝试使用基于行的协议("\n"在写入端附加 ,在读取端使用<>and chomp)但您的数据不是文本并且可以包含它自己"\n"的 s 所以您的读者停在第一个并砍掉它。

您需要使用其他一些方法来发出序列化数据结束的信号,例如,您可以在写入端关闭管道并继续运行,直到读取端出现 EOF。事实上,Storable有一对专门针对这种情况设计的函数:store_fdfd_retrieve. 他们将在没有 EOF 的情况下检测结束的方式进行传输,因此您可以保持管道打开以进行更多传输。

fd这是使用以下功能的程序的一个版本Storable

if ($fork == 0) { # actual fork scope
  $pipe_to_fork->reader();
  my %hash_rebuild = %{fd_retrieve($pipe_to_fork)}; # deserialize the retrieved serialized hash
  use Data::Dumper;$Data::Dumper::Useqq=1;print Dumper \%hash_rebuild;
  exit;
}

my %hash = ('key1' => "val1", 'key2' => "val2");

$pipe_to_fork->writer();
$pipe_to_fork->autoflush(1);

store_fd(\%hash, $pipe_to_fork);
于 2013-11-04T13:01:00.210 回答
4

问题是您假设散列在freezed一行中。但情况并非总是如此,因为$hash_serialized可能是包含多个\ns 的字符串。

因此,您应该阅读直到 EOF 并连接所有行,而不是只在子端读取一行。

#!/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;
  $hash_serialized .= $_ while (<$pipe_to_fork>);
  my %hash_rebuild = %{thaw($hash_serialized)}; # deserialize the retrieved serialized hash
  print $hash_rebuild{key1};
  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;

exit;

输出: val1

于 2013-11-04T13:11:53.673 回答