File Coverage

blib/lib/Math/Matlab/Pool.pm
Criterion Covered Total %
statement 28 49 57.1
branch 3 22 13.6
condition 0 3 0.0
subroutine 8 11 72.7
pod 5 5 100.0
total 44 90 48.8


line stmt bran cond sub pod time code
1             package Math::Matlab::Pool;
2              
3 1     1   953 use strict;
  1         3  
  1         46  
4 1     1   5 use vars qw($VERSION $MEMBERS $SYNC_FILE);
  1         3  
  1         109  
5              
6             BEGIN {
7 1     1   26 $VERSION = sprintf "%d.%03d", q$Revision: 1.4 $ =~ /: (\d+)\.(\d+)/;
8             }
9              
10 1     1   903 use Math::Matlab;
  1         3  
  1         29  
11 1     1   6 use base qw( Math::Matlab );
  1         3  
  1         107  
12              
13 1     1   6 use Fcntl qw(:DEFAULT :flock);
  1         4  
  1         1505  
14              
15             ##----- assign defaults, unless already set externally -----
16             $MEMBERS = [] unless defined $MEMBERS;
17             $SYNC_FILE = '/tmp/MatlabPool.lock' unless defined $SYNC_FILE;
18              
19             ##----- Public Class Methods -----
20             sub new {
21 1     1 1 477 my ($class, $href) = @_;
22 1 50       12 my $self = {
    50          
23             members => defined($href->{members}) ? $href->{members} : $MEMBERS,
24             sync_file => defined($href->{sync_file}) ? $href->{sync_file} : $SYNC_FILE,
25             err_msg => '',
26             result => ''
27             };
28              
29 1         3 bless $self, $class;
30              
31             ## create objects from config as necessary
32 1         3 foreach my $i ( 0..$#{$self->members} ) {
  1         4  
33 3         7 my $member = $self->members->[$i];
34 3 50       13 next unless ref $member eq 'HASH';
35 3         7 my $class = $member->{class};
36 3         14 $self->members->[$i] = $class->new( $member->{args} );
37             }
38              
39 1         5 return $self;
40             }
41              
42             ##----- Public Object Methods -----
43             sub execute {
44 0     0 1 0 my ($self, $code, $rel_mwd) = @_;
45              
46 0         0 my $matlab = $self->members->[ $self->next_index ];
47            
48 0 0       0 if ($matlab->execute($code, $rel_mwd)) {
49 0         0 $self->{'result'} = $matlab->fetch_raw_result;
50 0         0 return 1;
51             } else {
52 0         0 $self->err_msg( $matlab->err_msg );
53 0         0 return 0;
54             }
55             }
56              
57             sub next_index {
58 0     0 1 0 my ($self) = @_;
59            
60             ## the following code from p. 247 of Perl Cookbook
61 0 0       0 sysopen(FH, $self->sync_file, O_RDWR|O_CREAT)
62             or die "can't open syncfile: $!";
63 0 0       0 flock(FH, LOCK_EX) or die "can't lock syncfile: $!";
64             ## now we have the lock, let's do our stuff
65 0   0     0 my $num = || $#{$self->members};
66 0 0       0 seek(FH, 0, 0) or die "can't rewind syncfile: $!";
67 0 0       0 truncate(FH, 0) or die "can't truncate syncfile: $!";
68 0         0 $num++;
69 0 0       0 $num = 0 if $num > $#{$self->members};
  0         0  
70 0 0       0 print FH $num, "\n" or die "can't write syncfile: $!";
71 0 0       0 close(FH) or die "can't close syncfile: $!";
72              
73 0         0 return $num;
74             }
75              
76 7     7 1 10 sub members { my $self = shift; return $self->_getset('members', @_); }
  7         22  
77 0     0 1   sub sync_file { my $self = shift; return $self->_getset('sync_file', @_); }
  0            
78              
79             1;
80             __END__