File Coverage

blib/lib/MCE/Subs.pm
Criterion Covered Total %
statement 91 164 55.4
branch 5 16 31.2
condition 1 9 11.1
subroutine 9 51 17.6
pod 40 40 100.0
total 146 280 52.1


line stmt bran cond sub pod time code
1             ###############################################################################
2             ## ----------------------------------------------------------------------------
3             ## Exports functions mapped directly to MCE methods.
4             ##
5             ###############################################################################
6              
7             package MCE::Subs;
8              
9 1     1   1911 use strict;
  1         2  
  1         44  
10 1     1   6 use warnings;
  1         2  
  1         86  
11              
12 1     1   6 no warnings qw( threads recursion uninitialized );
  1         2  
  1         76  
13              
14             our $VERSION = '1.902';
15              
16             ## no critic (Subroutines::ProhibitSubroutinePrototypes)
17             ## no critic (TestingAndDebugging::ProhibitNoStrict)
18              
19 1     1   6 use MCE;
  1         4  
  1         8  
20 1     1   5 use MCE::Relay;
  1         2  
  1         27  
21              
22             ###############################################################################
23             ## ----------------------------------------------------------------------------
24             ## Import routine.
25             ##
26             ###############################################################################
27              
28             sub import {
29              
30 1     1   19 shift;
31              
32 1         2 my $_g_flg = 0; my $_m_flg = 0; my $_w_flg = 0;
  1         3  
  1         4  
33 1     0   6 my $_flag = sub { 1 }; my $_package = caller;
  0         0  
  1         6  
34              
35             ## Process module arguments.
36 1         6 while (my $_argument = shift) {
37 0         0 my $_arg = lc $_argument;
38              
39 0 0 0     0 $_g_flg = $_flag->() and next if ( $_arg eq ':getter' );
40 0 0 0     0 $_m_flg = $_flag->() and next if ( $_arg eq ':manager' );
41 0 0 0     0 $_w_flg = $_flag->() and next if ( $_arg eq ':worker' );
42              
43 0         0 _croak("Error: ($_argument) invalid module option");
44             }
45              
46 1 50       5 $_m_flg = $_w_flg = 1 if ($_m_flg + $_w_flg == 0);
47              
48 1         6 _export_subs($_package, $_g_flg, $_m_flg, $_w_flg);
49              
50 1         29 return;
51             }
52              
53             ###############################################################################
54             ## ----------------------------------------------------------------------------
55             ## Define functions.
56             ##
57             ###############################################################################
58              
59             ## Callable by the manager process only.
60              
61             sub mce_restart_worker (@) {
62 0     0 1 0 return $MCE::MCE->restart_worker(@_);
63             }
64              
65 0     0 1 0 sub mce_forchunk (@) { return $MCE::MCE->forchunk(@_); }
66 0     0 1 0 sub mce_foreach (@) { return $MCE::MCE->foreach(@_); }
67 0     0 1 0 sub mce_forseq (@) { return $MCE::MCE->forseq(@_); }
68 0     0 1 0 sub mce_process (@) { return $MCE::MCE->process(@_); }
69 0     0 1 0 sub mce_relay_final ( ) { return $MCE::MCE->relay_final(); }
70 0     0 1 0 sub mce_run (@) { return $MCE::MCE->run(@_); }
71 0     0 1 0 sub mce_send (@) { return $MCE::MCE->send(@_); }
72 0     0 1 0 sub mce_shutdown ( ) { return $MCE::MCE->shutdown(); }
73 0     0 1 0 sub mce_spawn ( ) { return $MCE::MCE->spawn(); }
74 0     0 1 0 sub mce_status ( ) { return $MCE::MCE->status(); }
75              
76             ## Callable by the worker process only.
77              
78 0     0 1 0 sub mce_exit (@) { return $MCE::MCE->exit(@_); }
79 0     0 1 0 sub mce_gather (@) { return $MCE::MCE->gather(@_); }
80 0     0 1 0 sub mce_last ( ) { return $MCE::MCE->last(); }
81 0     0 1 0 sub mce_next ( ) { return $MCE::MCE->next(); }
82 0     0 1 0 sub mce_relay (;&) { return $MCE::MCE->relay(@_); }
83 0     0 1 0 sub mce_relay_recv ( ) { return $MCE::MCE->relay_recv(); }
84 0     0 1 0 sub mce_sendto (;*@) { return $MCE::MCE->sendto(@_); }
85 0     0 1 0 sub mce_sync ( ) { return $MCE::MCE->sync(); }
86 0     0 1 0 sub mce_yield ( ) { return $MCE::MCE->yield(); }
87              
88             ## Callable by both the manager and worker processes.
89              
90 0     0 1 0 sub mce_abort ( ) { return $MCE::MCE->abort(); }
91 0     0 1 0 sub mce_do (@) { return $MCE::MCE->do(@_); }
92 0     0 1 0 sub mce_freeze (@) { return $MCE::MCE->{freeze}(@_); }
93 0     0 1 0 sub mce_print (;*@) { return $MCE::MCE->print(@_); }
94 0     0 1 0 sub mce_printf (;*@) { return $MCE::MCE->printf(@_); }
95 0     0 1 0 sub mce_say (;*@) { return $MCE::MCE->say(@_); }
96 0     0 1 0 sub mce_thaw (@) { return $MCE::MCE->{thaw}(@_); }
97              
98             ## Callable by both the manager and worker processes.
99              
100 0     0 1 0 sub mce_chunk_id ( ) { return $MCE::MCE->chunk_id(); }
101 0     0 1 0 sub mce_chunk_size ( ) { return $MCE::MCE->chunk_size(); }
102 0     0 1 0 sub mce_max_retries ( ) { return $MCE::MCE->max_retries(); }
103 0     0 1 0 sub mce_max_workers ( ) { return $MCE::MCE->max_workers(); }
104 0     0 1 0 sub mce_pid ( ) { return $MCE::MCE->pid(); }
105 0     0 1 0 sub mce_seed ( ) { return $MCE::MCE->seed(); }
106 0     0 1 0 sub mce_sess_dir ( ) { return $MCE::MCE->sess_dir(); }
107 0     0 1 0 sub mce_task_id ( ) { return $MCE::MCE->task_id(); }
108 0     0 1 0 sub mce_task_name ( ) { return $MCE::MCE->task_name(); }
109 0     0 1 0 sub mce_task_wid ( ) { return $MCE::MCE->task_wid(); }
110 0     0 1 0 sub mce_tmp_dir ( ) { return $MCE::MCE->tmp_dir(); }
111 0     0 1 0 sub mce_user_args ( ) { return $MCE::MCE->user_args(); }
112 0     0 1 0 sub mce_wid ( ) { return $MCE::MCE->wid(); }
113              
114             ###############################################################################
115             ## ----------------------------------------------------------------------------
116             ## Private methods.
117             ##
118             ###############################################################################
119              
120             sub _croak {
121              
122 0     0   0 goto &MCE::_croak;
123             }
124              
125             sub _export_subs {
126              
127 1     1   4 my ($_package, $_g_flg, $_m_flg, $_w_flg) = @_;
128              
129 1     1   10 no strict 'refs'; no warnings 'redefine';
  1     1   3  
  1         65  
  1         7  
  1         3  
  1         843  
130              
131             ## Callable by the manager process only.
132              
133 1 50       4 if ($_m_flg) {
134 1         2 *{ $_package . '::mce_restart_worker' } = \&mce_restart_worker;
  1         8  
135              
136 1         3 *{ $_package . '::mce_forchunk' } = \&mce_forchunk;
  1         5  
137 1         3 *{ $_package . '::mce_foreach' } = \&mce_foreach;
  1         5  
138 1         2 *{ $_package . '::mce_forseq' } = \&mce_forseq;
  1         5  
139 1         2 *{ $_package . '::mce_process' } = \&mce_process;
  1         5  
140 1         3 *{ $_package . '::mce_relay_final' } = \&mce_relay_final;
  1         4  
141 1         2 *{ $_package . '::mce_run' } = \&mce_run;
  1         5  
142 1         2 *{ $_package . '::mce_send' } = \&mce_send;
  1         5  
143 1         2 *{ $_package . '::mce_shutdown' } = \&mce_shutdown;
  1         11  
144 1         2 *{ $_package . '::mce_spawn' } = \&mce_spawn;
  1         4  
145 1         139 *{ $_package . '::mce_status' } = \&mce_status;
  1         83  
146             }
147              
148             ## Callable by the worker process only.
149              
150 1 50       8 if ($_w_flg) {
151 1         2 *{ $_package . '::mce_exit' } = \&mce_exit;
  1         6  
152 1         3 *{ $_package . '::mce_gather' } = \&mce_gather;
  1         4  
153 1         2 *{ $_package . '::mce_last' } = \&mce_last;
  1         3  
154 1         2 *{ $_package . '::mce_next' } = \&mce_next;
  1         3  
155 1         2 *{ $_package . '::mce_relay' } = \&mce_relay;
  1         4  
156 1         2 *{ $_package . '::mce_relay_recv' } = \&mce_relay_recv;
  1         4  
157 1         3 *{ $_package . '::mce_sendto' } = \&mce_sendto;
  1         4  
158 1         2 *{ $_package . '::mce_sync' } = \&mce_sync;
  1         5  
159 1         1 *{ $_package . '::mce_yield' } = \&mce_yield;
  1         5  
160             }
161              
162             ## Callable by both the manager and worker processes.
163              
164 1 50 33     5 if ($_m_flg || $_w_flg) {
165 1         2 *{ $_package . '::mce_abort' } = \&mce_abort;
  1         3  
166 1         2 *{ $_package . '::mce_do' } = \&mce_do;
  1         4  
167 1         2 *{ $_package . '::mce_freeze' } = \&mce_freeze;
  1         3  
168 1         2 *{ $_package . '::mce_print' } = \&mce_print;
  1         4  
169 1         35 *{ $_package . '::mce_printf' } = \&mce_printf;
  1         6  
170 1         3 *{ $_package . '::mce_say' } = \&mce_say;
  1         4  
171 1         2 *{ $_package . '::mce_thaw' } = \&mce_thaw;
  1         4  
172             }
173              
174 1 50       4 if ($_g_flg) {
175 0         0 *{ $_package . '::mce_chunk_id' } = \&mce_chunk_id;
  0         0  
176 0         0 *{ $_package . '::mce_chunk_size' } = \&mce_chunk_size;
  0         0  
177 0         0 *{ $_package . '::mce_max_retries' } = \&mce_max_retries;
  0         0  
178 0         0 *{ $_package . '::mce_max_workers' } = \&mce_max_workers;
  0         0  
179 0         0 *{ $_package . '::mce_pid' } = \&mce_pid;
  0         0  
180 0         0 *{ $_package . '::mce_seed' } = \&mce_seed;
  0         0  
181 0         0 *{ $_package . '::mce_sess_dir' } = \&mce_sess_dir;
  0         0  
182 0         0 *{ $_package . '::mce_task_id' } = \&mce_task_id;
  0         0  
183 0         0 *{ $_package . '::mce_task_name' } = \&mce_task_name;
  0         0  
184 0         0 *{ $_package . '::mce_task_wid' } = \&mce_task_wid;
  0         0  
185 0         0 *{ $_package . '::mce_tmp_dir' } = \&mce_tmp_dir;
  0         0  
186 0         0 *{ $_package . '::mce_user_args' } = \&mce_user_args;
  0         0  
187 0         0 *{ $_package . '::mce_wid' } = \&mce_wid;
  0         0  
188             }
189              
190 1         2 return;
191             }
192              
193             1;
194              
195             __END__
196              
197             ###############################################################################
198             ## ----------------------------------------------------------------------------
199             ## Module usage.
200             ##
201             ###############################################################################
202              
203             =head1 NAME
204              
205             MCE::Subs - Exports functions mapped directly to MCE methods
206              
207             =head1 VERSION
208              
209             This document describes MCE::Subs version 1.902
210              
211             =head1 SYNOPSIS
212              
213             use MCE::Subs; ## Exports manager and worker functions only
214             ## Getter functions are not exported by default
215              
216             use MCE::Subs qw( :getter ); ## All, including getter functions
217             use MCE::Subs qw( :manager ); ## Exports manager functions only
218             use MCE::Subs qw( :worker ); ## Exports worker functions only
219              
220             use MCE::Subs qw( :getter :worker ); ## Excludes manager functions
221              
222             =head1 DESCRIPTION
223              
224             This module exports functions mapped to MCE methods. All exported functions
225             are prototyped, therefore allowing one to call them without using parentheses.
226              
227             use MCE::Subs qw( :worker );
228              
229             sub user_func {
230             my $wid = MCE->wid;
231              
232             mce_say "A: $wid";
233             mce_sync;
234              
235             mce_say "B: $wid";
236             mce_sync;
237              
238             mce_say "C: $wid";
239             mce_sync;
240              
241             return;
242             }
243              
244             MCE->new(
245             max_workers => 24, user_func => \&user_func
246             );
247              
248             mce_run 0 for (1..100); ## 0 means do not shutdown after running
249              
250             For the next example, we only want the worker functions to be exported due
251             to using MCE::Map, which takes care of creating a MCE instance and running.
252              
253             use MCE::Map;
254             use MCE::Subs qw( :worker );
255              
256             ## The following serializes output to STDOUT and gathers $_ to @a.
257             ## mce_say displays $_ when called without arguments.
258              
259             my @a = mce_map { mce_say; $_ } 1 .. 100;
260              
261             print scalar @a, "\n";
262              
263             Unlike the native Perl functions, printf, print, and say methods require the
264             comma after the glob reference or file handle.
265              
266             MCE->printf(\*STDERR, "%s\n", $error_msg);
267             MCE->print(\*STDERR, $error_msg, "\n");
268             MCE->say(\*STDERR, $error_msg);
269             MCE->say($fh, $error_msg);
270              
271             mce_printf \*STDERR, "%s\n", $error_msg;
272             mce_print \*STDERR, $error_msg, "\n";
273             mce_say \*STDERR, $error_msg;
274             mce_say $fh, $error_msg;
275              
276             =head1 FUNCTIONS for the MANAGER PROCESS via ( :manager )
277              
278             MCE methods are described in L<MCE::Core>.
279              
280             =over 3
281              
282             =item * mce_abort
283              
284             =item * mce_do
285              
286             =item * mce_forchunk
287              
288             =item * mce_foreach
289              
290             =item * mce_forseq
291              
292             =item * mce_freeze
293              
294             =item * mce_process
295              
296             =item * mce_relay_final
297              
298             =item * mce_restart_worker
299              
300             =item * mce_run
301              
302             =item * mce_print
303              
304             =item * mce_printf
305              
306             =item * mce_say
307              
308             =item * mce_send
309              
310             =item * mce_shutdown
311              
312             =item * mce_spawn
313              
314             =item * mce_status
315              
316             =item * mce_thaw
317              
318             =back
319              
320             =head1 FUNCTIONS for MCE WORKERS via ( :worker )
321              
322             MCE methods are described in L<MCE::Core>.
323              
324             =over 3
325              
326             =item * mce_abort
327              
328             =item * mce_do
329              
330             =item * mce_exit
331              
332             =item * mce_freeze
333              
334             =item * mce_gather
335              
336             =item * mce_last
337              
338             =item * mce_next
339              
340             =item * mce_print
341              
342             =item * mce_printf
343              
344             =item * mce_relay
345              
346             =item * mce_relay_recv
347              
348             =item * mce_say
349              
350             =item * mce_sendto
351              
352             =item * mce_sync
353              
354             =item * mce_thaw
355              
356             =item * mce_yield
357              
358             =back
359              
360             =head1 GETTERS for MCE ATTRIBUTES via ( :getter )
361              
362             MCE methods are described in L<MCE::Core>.
363              
364             =over 3
365              
366             =item * mce_chunk_id
367              
368             =item * mce_chunk_size
369              
370             =item * mce_max_retries
371              
372             =item * mce_max_workers
373              
374             =item * mce_pid
375              
376             =item * mce_seed
377              
378             =item * mce_sess_dir
379              
380             =item * mce_task_id
381              
382             =item * mce_task_name
383              
384             =item * mce_task_wid
385              
386             =item * mce_tmp_dir
387              
388             =item * mce_user_args
389              
390             =item * mce_wid
391              
392             =back
393              
394             =head1 INDEX
395              
396             L<MCE|MCE>, L<MCE::Core>
397              
398             =head1 AUTHOR
399              
400             Mario E. Roy, S<E<lt>marioeroy AT gmail DOT comE<gt>>
401              
402             =cut
403