File Coverage

blib/lib/Command/Run/Tmpfile.pm
Criterion Covered Total %
statement 60 60 100.0
branch 8 14 57.1
condition 3 5 60.0
subroutine 16 16 100.0
pod 8 8 100.0
total 95 103 92.2


line stmt bran cond sub pod time code
1             package Command::Run::Tmpfile;
2              
3 30     30   314923 use v5.14;
  30         131  
4 30     30   106 use warnings;
  30         59  
  30         1263  
5 30     30   1108 use utf8;
  30         449  
  30         127  
6 30     30   751 use Carp;
  30         35  
  30         2061  
7 30     30   133 use Fcntl;
  30         34  
  30         5340  
8 30     30   880 use IO::File;
  30         14032  
  30         4804  
9 30     30   140 use IO::Handle;
  30         83  
  30         14469  
10              
11             my $fdpath;
12              
13             sub new {
14 213     213 1 137108 my $class = shift;
15 213         568 my %opt = @_;
16 213 50       59566 my $fh = new_tmpfile IO::File or die "new_tmpfile: $!\n";
17 213 50       2418 $fh->fcntl(F_SETFD, 0) or die "fcntl F_SETFD: $!\n";
18 213 100   29   6721 binmode $fh, $opt{raw} ? ':raw' : ':encoding(utf8)';
  29         16607  
  29         378  
  29         127  
19             # Determine usable fd-path on first instantiation, using the fd we
20             # just allocated. Checking only "$path/0" is insufficient on
21             # FreeBSD where /dev/fd/0,1,2 always exist as device nodes but
22             # /dev/fd/N (N>2) requires fdescfs to be mounted.
23 213   66     32943 $fdpath //= do {
24 29         227 my $fd = $fh->fileno;
25 29         143 my $found;
26 29         72 for my $path (qw(/proc/self/fd /dev/fd)) {
27 29 50       1410 -r "$path/$fd" and do { $found = $path; last };
  29         68  
  29         58  
28             }
29 29   50     162 $found // '';
30             };
31 213         1536 bless { FH => $fh }, $class;
32             }
33              
34             sub write {
35 5     5 1 506 my $obj = shift;
36 5         10 my $fh = $obj->fh;
37 5 50       10 if (@_) {
38 5         10 my $data = join '', @_;
39 5         13 $fh->print($data);
40             }
41 5         51 $obj;
42             }
43              
44             sub flush {
45 4     4 1 5 my $obj = shift;
46 4         5 $obj->fh->flush;
47 4         13 $obj;
48             }
49              
50             sub rewind {
51 5     5 1 7 my $obj = shift;
52 5 50       8 $obj->fh->seek(0, 0) or die "seek: $!\n";
53 5         43 $obj;
54             }
55              
56             sub reset {
57 1     1 1 700 my $obj = shift;
58 1         3 $obj->rewind;
59 1         3 $obj->fh->truncate(0);
60 1         71 $obj;
61             }
62              
63             sub fh {
64 202     202 1 1285 my $obj = shift;
65 202         1180 $obj->{FH};
66             }
67              
68             sub fd {
69 11     11 1 263 my $obj = shift;
70 11         151 $obj->fh->fileno;
71             }
72              
73             sub path {
74 9     9 1 1609 my $obj = shift;
75 9 50       80 return undef unless $fdpath;
76 9         137 sprintf "%s/%d", $fdpath, $obj->fd;
77             }
78              
79             1;
80              
81             __END__