私のコードの何が問題なのかわかりません。親内のハッシュをシリアル化し、それをフォークにパイプして、逆シリアル化する必要があります。
#!/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";
論理的な違いはあまりありませんね。誰かが私にこの行動についてもっと説明してくれたらいいのにと思います。