File Coverage

lib/IPC/SRLock/Utils.pm
Criterion Covered Total %
statement 39 40 100.0
branch 13 14 100.0
condition 5 5 100.0
subroutine 12 12 100.0
pod 5 5 100.0
total 74 76 100.0


line stmt bran cond sub pod time code
1             package IPC::SRLock::Utils;
2              
3 1     1   289566 use strict;
  1         2  
  1         27  
4 1     1   3 use warnings;
  1         1  
  1         20  
5 1     1   3 use parent 'Exporter::Tiny';
  1         1  
  1         4  
6              
7 1     1   421 use IPC::SRLock::Constants qw( EXCEPTION_CLASS );
  1         2  
  1         8  
8 1     1   148 use Scalar::Util qw( blessed );
  1         1  
  1         299  
9              
10             our @EXPORT_OK = qw( Unspecified hash_from loop_until merge_attributes throw );
11              
12             sub Unspecified () {
13 1     1 1 5 return sub { 'Unspecified' };
  1     1   3036  
14             }
15              
16             sub hash_from (;@) {
17 15 100   15 1 24 my (@args) = @_; $args[ 0 ] or return {};
  15         35  
18              
19 14 100       53 return ref $args[ 0 ] ? $args[ 0 ] : { @args };
20             }
21              
22             sub loop_until ($) {
23 7     7 1 9 my $f = shift;
24              
25             return sub {
26 7     7   8 my $self = shift; my $args = $self->_get_args( @_ ); my $start = time;
  7         27  
  6         7  
27              
28 6         6 while (1) {
29 6         9 my $now = time;
30 6 100       23 my $r = $f->( $self, $args, $now ); $r and return $r;
  6         36  
31              
32             # uncoverable branch false
33 2 50       12 $args->{async} and return 0;
34             # uncoverable statement
35 0         0 $self->_sleep_or_timeout( $start, $now, $self->lockfile );
36             }
37 7         32 };
38             }
39              
40             sub merge_attributes ($$;$) {
41 3     3 1 4 my ($dest, $src, $keys) = @_; my $class = blessed $src;
  3         10  
42              
43 3   100     3 for (grep { not exists $dest->{ $_ } or not defined $dest->{ $_ } }
  5         15  
44 3   100     16 @{ $keys // [] }) {
45 4 100       29 my $v = $class ? ($src->can( $_ ) ? $src->$_() : undef) : $src->{ $_ };
    100          
46              
47 4 100       9 defined $v and $dest->{ $_ } = $v;
48             }
49              
50 3         5 return $dest;
51             }
52              
53             sub throw (;@) {
54 5     5 1 29 EXCEPTION_CLASS->throw( @_ );
55             }
56              
57             1;
58              
59             __END__
60              
61             =pod
62              
63             =encoding utf-8
64              
65             =head1 Name
66              
67             IPC::SRLock::Utils - Common functions used by this distribution
68              
69             =head1 Synopsis
70              
71             use IPC::SRLock::Utils qw( Unspecified hash_from get_args );
72              
73             =head1 Description
74              
75             Common functions used by this distribution
76              
77             =head1 Subroutines/Methods
78              
79             =head2 Unspecified
80              
81             Returns a subroutine reference which when called returns the string
82             C<Unspecified>. This is an exception class used as an argument to the
83             L<throw|IPC::SRLock::Base/throw> method
84              
85             =head2 hash_from
86              
87             Returns a hash reference. Accepts a hash reference or a list of keys and
88             values
89              
90             =head2 loop_until
91              
92             Loop until the closed over subroutine returns true or a timeout occurs
93              
94             =head2 merge_attributes
95              
96             $dest = merge_attributes $dest, $src, $attr_list_ref;
97              
98             Merges attribute hashes. The C<$dest> hash is updated and
99             returned. The C<$dest> hash values take precedence over the C<$src>
100             hash values. The C<$src> hash may be an object in which case its
101             accessor methods are called
102              
103             =head2 throw
104              
105             Expose the C<throw> method in L<File::DataClass::Exception>
106              
107             =head1 Configuration and Environment
108              
109             None
110              
111             =head1 Diagnostics
112              
113             None
114              
115             =head1 Dependencies
116              
117             =over 3
118              
119             =item L<Exporter::Tiny>
120              
121             =back
122              
123             =head1 Incompatibilities
124              
125             There are no known incompatibilities in this module
126              
127             =head1 Bugs and Limitations
128              
129             There are no known bugs in this module. Please report problems to
130             http://rt.cpan.org/NoAuth/Bugs.html?Dist=IPC-SRLock.
131             Patches are welcome
132              
133             =head1 Acknowledgements
134              
135             Larry Wall - For the Perl programming language
136              
137             =head1 Author
138              
139             Peter Flanigan, C<< <pjfl@cpan.org> >>
140              
141             =head1 License and Copyright
142              
143             Copyright (c) 2016 Peter Flanigan. All rights reserved
144              
145             This program is free software; you can redistribute it and/or modify it
146             under the same terms as Perl itself. See L<perlartistic>
147              
148             This program is distributed in the hope that it will be useful,
149             but WITHOUT WARRANTY; without even the implied warranty of
150             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
151              
152             =cut
153              
154             # Local Variables:
155             # mode: perl
156             # tab-width: 3
157             # End:
158             # vim: expandtab shiftwidth=3: