我不知道什麼是錯我的代碼。我試圖序列化一個哈希內部的散列,並將其傳遞給叉,它應該被反序列化。的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