The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Patro::CODE::Shareable - manipulate a code reference so it can be shared across threads

SYNOPSIS

    use threads;
    use threads::shared;
    use Patro::CODE::Shareable;

    my $code = sub { print "This is an anonymous sub" };
    share($code);
    my $dispatch_table = shared_clone( { action => $code } );

DESCRIPTION

The default sharing mechanism in threads::shared does not work for CODE references.

    use threads;
    use threads::shared;
    my $sub : shared = sub { "anonymous code ref" };
    # "Invalid value for shared scalar at ... line 3"

    use threads;
    use threads::shared;
    my $dispatch = {
        foo => sub { "taking action foo" },
        bar => sub { "taking action bar" }
    };
    $dispatch = shared_clone($dispatch);
    # "Unsupported ref type: CODE at ... line 7"

Patro::CODE::Shareable describes a proxy object for a code reference that overloads the code dereferencing operator to have the look and feel of a CODE reference, but since it's not really a CODE reference, it can be shared between threads.

Where possible, this module should be loaded after threads::shared is loaded.

FUNCTIONS

share(SCALAR)

share(ARRAY)

share(HASH)

share(REF)

Like threads::shared\"share", enables the variable in the input to be shared across threads. Unlike threads::shared::share, this function also works when the input holds a CODE reference.

$copy = shared_clone($data)

Like threads::shared\"shared_clone", performs a deep inspection of the input data structure and make any references within the data structure shared across threads. Unlike threads::shared::shared_clone, this function supports the sharing of CODE references within the data structure.

LIMITATIONS

Using this module requires features of Perl that only work properly on Perl v5.17.0 or better. If the code $shared_coderef-(@args)> gives you a Not a CODE reference ... error, a workaround is to call $shared_coderef-_invoke->(@args)>. This is how the Patro distribution works around this problem for older versions of perl.

LICENSE AND COPYRIGHT

MIT License

Copyright (c) 2017, Marty O'Brien

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.