File Coverage

lib/File/Takeput/Unix.pm
Criterion Covered Total %
statement 27 28 96.4
branch 1 2 50.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 33 37 89.1


line stmt bran cond sub pod time code
1             # File::Takeput::Unix.pm
2             # Used by the File::Takeput module.
3             # (c) 2023 Bjørn Hee
4             # Licensed under the Apache License, version 2.0
5             # https://www.apache.org/licenses/LICENSE-2.0.txt
6              
7             package File::Takeput::Unix;
8              
9 6     6   81 use strict;
  6         15  
  6         264  
10 6     6   33 use experimental qw(signatures);
  6         11  
  6         50  
11 6     6   779 use Exporter qw(import);
  6         12  
  6         340  
12              
13 6     6   35 use Time::HiRes qw(ualarm);
  6         11  
  6         57  
14              
15             our @EXPORT = qw(flock_take);
16              
17             our @EXPORT_OK = ();
18              
19             # --------------------------------------------------------------------------- #
20              
21             1;
22              
23             # --------------------------------------------------------------------------- #
24             # Exportable subs.
25              
26 29     29 0 48 sub flock_take( $fh , $flag , $p ) {
  29         83  
  29         45  
  29         42  
  29         79  
27              
28 29         50 $p *= 1000000; # Seconds to microseconds.
29              
30 29         76 my $ret = undef;
31 29         54 eval {
32 29     0   451 local $SIG{ALRM} = sub { exit; };
  0         0  
33 29         63 $@ = '';
34 29         216 ualarm $p;
35 29 50       80 exit if $@ ne '';
36 29         248 $ret = flock($fh , $flag);
37 29         359 ualarm 0; # Cancel timer.
38             };
39 29         119 return $ret;
40             };
41              
42             # --------------------------------------------------------------------------- #
43              
44             __END__