| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # (C) Paul Evans, 2011-2024 -- leonerd@leonerd.org.uk | ||||||||||||||
| 5 | |||||||||||||||
| 6 | package IO::Async::Internals::FunctionWorker 0.805; | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 11 | ▶ | use v5.14; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 11 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | # Called directly by IO::Async::Function::Worker when used in "code" mode, | ||||||||||||||
| 12 | # or by run_worker() below. | ||||||||||||||
| 13 | sub runloop | ||||||||||||||
| 14 | { | ||||||||||||||
| 15 | 2 | ▶ | my ( $code, $arg_channel, $ret_channel ) = @_; | ||||||||||||
|
Subroutine
runloop: called CC=4 SCAR=16.0
Pod
undocumented
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | ▶ | while( my $args = $arg_channel->recv ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 18 | 2 | my @ret; | |||||||||||||
| 19 | 2 | my $ok = eval { @ret = $code->( @$args ); 1 }; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | ▶ | if( $ok ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 22 | 1 | $ret_channel->send( [ r => @ret ] ); | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | elsif( ref $@ ) { | ||||||||||||||
| 25 | # Presume that $@ is an ARRAYref of error results | ||||||||||||||
| 26 | 0 | $ret_channel->send( [ e => @{ $@ } ] ); | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | else { | ||||||||||||||
| 29 | 0 | chomp( my $e = "$@" ); | |||||||||||||
| 30 | 0 | $ret_channel->send( [ e => $e, error => ] ); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | } | ||||||||||||||
| 33 | } | ||||||||||||||
| 34 | |||||||||||||||
| 35 | # Called by IO::Async::Function::Worker via the module+func arguments to its | ||||||||||||||
| 36 | # IO::Async::Routine superclass when used in "module+func" mode | ||||||||||||||
| 37 | sub run_worker | ||||||||||||||
| 38 | { | ||||||||||||||
| 39 | 1 | ▶ | my ( $arg_channel, $ret_channel ) = @_; | ||||||||||||
|
Subroutine
run_worker: called CC=4 SCAR=15.8
Pod
undocumented
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | # Setup args | ||||||||||||||
| 42 | 1 | my ( $module, $func, $init_func, @init_args ) = @{ $arg_channel->recv }; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 1 | ( my $file = "$module.pm" ) =~ s{::}{/}g; | |||||||||||||
| 45 | 1 | require $file; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 1 | ▶ | my $code = $module->can( $func ) or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | die "Module $module does not provide a function called $func\n"; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 1 | ▶ | if( defined $init_func ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | ▶ | my $init = $module->can( $init_func ) or | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | die "Module $module does not provide a function called $init_func\n"; | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 0 | $init->( @init_args ); | |||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 1 | runloop( $code, $arg_channel, $ret_channel ); | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | |||||||||||||||
| 60 | 11 | 0x55AA; | |||||||||||||