It's trivial using the mighty MCE::Shared by marioroy, two extra lines:
package MyClass {
use Moose;
has foo => ( is => 'rw', default => 'bar' );
};
package main {
use strict; use warnings; use feature 'say';
use MCE::Shared;
my $obj = MCE::Shared->share({ module => 'MyClass' });
say 'Before forking: ' . $obj->foo;
my $pid = fork;
if ( $pid == 0 ) {
$obj->foo('baz');
exit;
} else {
waitpid $pid, 0;
}
say 'After forking: ' . $obj->foo;
};
Output:
$ perl 1210687.pl
Before forking: bar
After forking: baz
For details see
the documentation on sharing objects between processes with MCE::Shared.
Hope this helps!
The way forward always starts with a minimal test.