Test::...Object(3pm)

πŸ“¦
version 0.01 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

✨ 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 new Test::SharedObject instance, and sets $value as the initial value. Under the hood it creates a temporary file, serializes $value with 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:

  1. Lock temporary file.
  2. Read shared value.
  3. Executes \&coderef. (Set shared value as first arguments)
  4. Write return value as shared value.
  5. 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 $value as 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