小编car*_*rot的帖子

Perl:将序列化哈希管道传递给分叉进程

我不知道我的代码有什么问题.我正在尝试序列化父级内部的哈希并将其传递给fork,它应该在其中进行反序列化.

#!/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 …
Run Code Online (Sandbox Code Playgroud)

perl serialization fork storable

2
推荐指数
1
解决办法
523
查看次数

标签 统计

fork ×1

perl ×1

serialization ×1

storable ×1