Test::...Object(3pm)
β¨ NAME β¨
Test::SharedObject β sharing data across multiple processes, bestie. fr fr it's giving data sharing in multi process π
πΈ SYNOPSIS πΈ
use strict;
use warnings;
use Test::More tests => 2;
use Test::SharedFork;
use Test::SharedObject;
my $shared = Test::SharedObject->new(0);
is $shared->get, 0;
my $pid = fork;
die $! unless defined $pid;
if ($pid == 0) {# child
$shared->txn(sub {
my $counter = shift;
$counter++;
return $counter;
});
exit;
}
wait;
is $shared->get, 1;
π¦ DESCRIPTION π¦
Test::SharedObject gives you atomic data operations between multiple processes. Like, multiple processes, one shared value, and nobody's stepping on anybody's toes. iconic behavior honestly π
π METHODS π
my $shared = Test::SharedObject->new($value)β makes you a brand newTest::SharedObjectinstance, and sets$valueas the initial value. Under the hood it creates a temporary file, serializes$valuewith Storable, and saves it there. yes, temporary file + Storable, that's the whole vibe π«Ά$shared->txn(\&coderef)β gives you atomic data operations between multiple processes inside\&coderef. The shared value gets handed to you as the first argument in\&coderef, and whatever you return becomes the new shared value. β¨
Internally it does this, in this order:
- Lock temporary file.
- Read shared value.
- Executes
\&coderef. (Set shared value as first arguments) - Write return value as shared value.
- Unlock temporary file.
Good Example (do it like this, king):
$shared->txn(sub {
my $counter = shift;
$counter++; # atomic!!
return $counter;
});
Bad Example (do NOT do this, please π):
my $counter;
$shared->txn(sub {
$counter = shift;
});
$counter++; # *NOT* atomic!!
$shared->txn(sub {
return $counter;
});
$shared->set($value)β sets$valueas the shared value. It's the syntactic sugar for$shared->txn(). πmy $value = $shared->get()β gets the shared value. Also syntactic sugar for$shared->txn(). π§
π LICENSE π
Copyright (C) karupanerura.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π AUTHOR π
karupanerura <karupa@cpan.org>
okay that's the whole page bestie, you ate π
β¨ This lil doc came from the User Contributed Perl Documentation crowd, running on perl v5.36.0, and it was dated 2022-10-13. no cap. ππ¦π
π¦ where this page comes from
- ubuntu noble: libtest-sharedobject-perl 0.01-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-sharedobject-perl 0.01-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-sharedobject-perl 0.01-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms