File Coverage

blib/lib/IO/Async/Internals/FunctionWorker.pm
Criterion Covered Total %
statement 21 27 77.7
branch 3 10 30.0
condition n/a
subroutine 4 4 100.0
pod 0 2 0.0
total 28 43 65.1


line stmt bran cond sub pod time code
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     11   159848 use v5.14;
  11         38  
9 11     11   120 use warnings;
  11         18  
  11         4457  
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     2 0 7 my ( $code, $arg_channel, $ret_channel ) = @_;
16              
17 2         25 while( my $args = $arg_channel->recv ) {
18 2         6 my @ret;
19 2         12 my $ok = eval { @ret = $code->( @$args ); 1 };
  2         44  
  1         3  
20              
21 1 50       4 if( $ok ) {
    0          
22 1         36 $ret_channel->send( [ r => @ret ] );
23             }
24             elsif( ref $@ ) {
25             # Presume that $@ is an ARRAYref of error results
26 0         0 $ret_channel->send( [ e => @{ $@ } ] );
  0         0  
27             }
28             else {
29 0         0 chomp( my $e = "$@" );
30 0         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     1 0 3 my ( $arg_channel, $ret_channel ) = @_;
40              
41             # Setup args
42 1         3 my ( $module, $func, $init_func, @init_args ) = @{ $arg_channel->recv };
  1         4  
43              
44 1         8 ( my $file = "$module.pm" ) =~ s{::}{/}g;
45 1         6 require $file;
46              
47 1 50       16 my $code = $module->can( $func ) or
48             die "Module $module does not provide a function called $func\n";
49              
50 1 50       4 if( defined $init_func ) {
51 0 0       0 my $init = $module->can( $init_func ) or
52             die "Module $module does not provide a function called $init_func\n";
53              
54 0         0 $init->( @init_args );
55             }
56              
57 1         5 runloop( $code, $arg_channel, $ret_channel );
58             }
59              
60             0x55AA;