Shared::Hash - hash-like object which is shared between processes
use Shared::Hash;
my $hash = Shared::Hash->new;
my $pid = fork // die;
if ($pid == 0) {
# child
$hash->set(message => "from child!");
exit;
}
sleep 1;
print $hash->get("message"); # from child!
Shared::Hash is a hash-like object which is shared between processes. It uses a file for IPC.
-
support lock
$hash->lock(sub { # in this callback, your operations for $hash are atomic! my $i = $hash->get("foo"); $i++; $hash->set(foo => $i); });
-
hash may contain arbitrary perl data type
$hash->set(foo => { hash => "ref" }); $hash->set(bar => [1..10]);
Create a new Shared::Hash object.
You can optionally specify path
option.
Then, you can also use it later:
$ perl -MShared::Hash -e 'Shared::Hash->new(path => "foo.data")->set(foo => "bar")'
$ perl -MShared::Hash -e 'print(Shared::Hash->new(path => "foo.data")->get("foo"))'
bar
Default path
is a tempfile.
Get the value for $key
.
If $hash
does not contain $key
, then it returns undef
.
Set $value
for $key
.
Get a cloned hash reference.
All keys of $hash
.
All values of $hash
.
In $callback
, your operation for $hash
is atomic.
Copyright (C) Shoichi Kaji.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Shoichi Kaji skaji@cpan.org