File Coverage

blib/lib/Data/RoundRobinShared.pm
Criterion Covered Total %
statement 41 43 95.3
branch 5 10 50.0
condition 3 14 21.4
subroutine 8 8 100.0
pod 3 3 100.0
total 60 78 76.9


line stmt bran cond sub pod time code
1             package Data::RoundRobinShared;
2              
3 1     1   710 use strict;
  1         2  
  1         28  
4 1     1   5 use warnings;
  1         1  
  1         31  
5 1     1   912 use IPC::Shareable (':lock');
  1         23019  
  1         195  
6 1     1   10 use Carp;
  1         4  
  1         106  
7              
8             our $VERSION = '0.11';
9              
10             use overload
11 1         14 'eq' => \&next,
12             'ne' => \&next,
13 1     1   5 '""' => \&next;
  1         2  
14              
15             sub new
16             {
17 1     1 1 705 my $class = shift;
18 1         4 my %params = @_;
19              
20 1 50       5 croak("odd number of arguments passed") if ( @_ % 2 );
21 1 50       3 croak("data needs to be specified") unless ( exists( $params{data} ) );
22 1 50       4 croak("data needs to be arrayref") unless ( ref $params{data} eq 'ARRAY' );
23              
24 1         2 my $self = {};
25              
26 1   33     6 $class = ref $class || $class;
27              
28 1   50     6 my %options = ( key => $params{key} || 'sharedRoundRoundRobin', create => 1, mode => 0644 );
29              
30 1 50       8 tie $self->{data}, 'IPC::Shareable', $params{key}, {%options} or croak("server: tie failed");
31              
32 1 50 0     514 if ( !$self->{data} || ( ref $self->{data} eq 'ARRAY' && scalar( @{ $self->{data} } ) != scalar( @{ $params{data} } ) && $params{simple_check} ) )
  0   0     0  
  0   33     0  
33             {
34 1         179 ( tied $self->{data} )->shlock(LOCK_EX);
35 1         114 $self->{data} = [ @{ $params{data} } ];
  1         8  
36 1         1931 ( tied $self->{data} )->shlock(LOCK_UN);
37             }
38              
39 1         231 return bless $self, $class;
40             }
41              
42             sub next
43             {
44 2     2 1 6 my $self = shift;
45              
46 2         35 ( tied $self->{data} )->shlock(LOCK_EX);
47              
48 2         308 my $val = shift( @{ $self->{data} } );
  2         8  
49 2         890 push( @{ $self->{data} }, $val );
  2         7  
50              
51 2         713 ( tied $self->{data} )->shlock(LOCK_UN);
52              
53 2         59 return $val;
54             }
55              
56             sub remove
57             {
58 1     1 1 2 my $self = shift;
59              
60 1         6 ( tied $self->{data} )->remove;
61              
62 1         186 return 1;
63             }
64              
65             1;
66              
67             __END__