File Coverage

blib/lib/Command/Run/Tmpfile.pm
Criterion Covered Total %
statement 59 59 100.0
branch 6 12 50.0
condition 3 5 60.0
subroutine 16 16 100.0
pod 8 8 100.0
total 92 100 92.0


line stmt bran cond sub pod time code
1             package Command::Run::Tmpfile;
2              
3 29     29   328474 use v5.14;
  29         110  
4 29     29   152 use warnings;
  29         69  
  29         1600  
5 29     29   964 use utf8;
  29         429  
  29         158  
6 29     29   953 use Carp;
  29         32  
  29         1925  
7 29     29   222 use Fcntl;
  29         43  
  29         5971  
8 29     29   901 use IO::File;
  29         12772  
  29         5930  
9 29     29   269 use IO::Handle;
  29         43  
  29         15072  
10              
11             my $fdpath;
12              
13             sub new {
14 191     191 1 162066 my $class = shift;
15 191 50       60117 my $fh = new_tmpfile IO::File or die "new_tmpfile: $!\n";
16 191 50       2214 $fh->fcntl(F_SETFD, 0) or die "fcntl F_SETFD: $!\n";
17 191     28   5041 binmode $fh, ':encoding(utf8)';
  28         16959  
  28         389  
  28         112  
18             # Determine usable fd-path on first instantiation, using the fd we
19             # just allocated. Checking only "$path/0" is insufficient on
20             # FreeBSD where /dev/fd/0,1,2 always exist as device nodes but
21             # /dev/fd/N (N>2) requires fdescfs to be mounted.
22 191   66     30880 $fdpath //= do {
23 28         210 my $fd = $fh->fileno;
24 28         152 my $found;
25 28         75 for my $path (qw(/proc/self/fd /dev/fd)) {
26 28 50       1866 -r "$path/$fd" and do { $found = $path; last };
  28         76  
  28         57  
27             }
28 28   50     203 $found // '';
29             };
30 191         1166 bless { FH => $fh }, $class;
31             }
32              
33             sub write {
34 3     3 1 438 my $obj = shift;
35 3         5 my $fh = $obj->fh;
36 3 50       6 if (@_) {
37 3         14 my $data = join '', @_;
38 3         11 $fh->print($data);
39             }
40 3         21 $obj;
41             }
42              
43             sub flush {
44 2     2 1 3 my $obj = shift;
45 2         18 $obj->fh->flush;
46 2         30 $obj;
47             }
48              
49             sub rewind {
50 3     3 1 5 my $obj = shift;
51 3 50       7 $obj->fh->seek(0, 0) or die "seek: $!\n";
52 3         29 $obj;
53             }
54              
55             sub reset {
56 1     1 1 603 my $obj = shift;
57 1         3 $obj->rewind;
58 1         2 $obj->fh->truncate(0);
59 1         53 $obj;
60             }
61              
62             sub fh {
63 174     174 1 1560 my $obj = shift;
64 174         995 $obj->{FH};
65             }
66              
67             sub fd {
68 11     11 1 271 my $obj = shift;
69 11         59 $obj->fh->fileno;
70             }
71              
72             sub path {
73 9     9 1 843 my $obj = shift;
74 9 50       60 return undef unless $fdpath;
75 9         115 sprintf "%s/%d", $fdpath, $obj->fd;
76             }
77              
78             1;
79              
80             __END__