File Coverage

blib/lib/Crypt/Random/Source/Base/Proc.pm
Criterion Covered Total %
statement 34 39 87.1
branch 4 10 40.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 49 60 81.6


line stmt bran cond sub pod time code
1             package Crypt::Random::Source::Base::Proc;
2             # ABSTRACT: Base class for helper processes (e.g. C)
3              
4             our $VERSION = '0.11';
5              
6 1     1   26463 use Moo;
  1         14716  
  1         7  
7              
8             extends qw(Crypt::Random::Source::Base::Handle);
9              
10 1     1   3525 use Capture::Tiny 0.08 qw(capture);
  1         32504  
  1         68  
11 1     1   8 use File::Spec;
  1         2  
  1         23  
12 1     1   748 use IO::File 1.14;
  1         958  
  1         154  
13 1     1   937 use Types::Standard qw(Str);
  1         70870  
  1         9  
14 1     1   1582 use namespace::clean;
  1         12631  
  1         7  
15              
16 1     1   692 use 5.008;
  1         3  
17              
18             has command => ( is => "rw", required => 1 );
19             has search_path => ( is => 'rw', isa => Str, lazy => 1, builder => 1);
20              
21             # This is a scalar so that people can customize it (which they would
22             # particularly need to do on Windows).
23             our $TAINT_PATH =
24             '/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin';
25              
26             sub _build_search_path {
27             # In taint mode it's not safe to use $ENV{PATH}.
28 1 50   1   843 if (${^TAINT}) {
29 0         0 return $TAINT_PATH;
30             }
31 1         23 return $ENV{PATH};
32             }
33              
34             sub open_handle {
35 1     1 1 2 my $self = shift;
36              
37 1         11 my $cmd = $self->command;
38 1 50       5 my @cmd = ref $cmd ? @$cmd : $cmd;
39 1         2 my $retval;
40 1         5 local $ENV{PATH} = $self->search_path;
41 1     1   115 my ($stdout, $stderr) = capture { $retval = system(@cmd) };
  1         7698  
42 1         1121 chomp($stderr);
43 1 50       19 if ($retval) {
44 0         0 my $err = join(' ', @cmd) . ": $! ($?)";
45 0 0       0 if ($stderr) {
46 0         0 $err .= "\n$stderr";
47             }
48 0         0 die $err;
49             }
50 1 50       8 warn $stderr if $stderr;
51              
52 1         21 my $fh = IO::File->new(\$stdout, '<');
53              
54 1         1603 return $fh;
55             }
56              
57             1;
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Crypt::Random::Source::Base::Proc - Base class for helper processes (e.g. C)
66              
67             =head1 VERSION
68              
69             version 0.11
70              
71             =head1 SYNOPSIS
72              
73             use Moose;
74              
75             extends qw(Crypt::Random::Source::Base::Proc);
76              
77             has '+command' => ( default => ... );
78              
79             =head1 DESCRIPTION
80              
81             This is a base class for using command line utilities which output random data
82             on STDOUT as L objects.
83              
84             =head1 ATTRIBUTES
85              
86             =head2 command
87              
88             An array reference or string that is the command to run.
89              
90             =head1 METHODS
91              
92             =head2 open_handle
93              
94             Opens a pipe for reading using C.
95              
96             =head1 SUPPORT
97              
98             Bugs may be submitted through L
99             (or L).
100              
101             =head1 AUTHOR
102              
103             יובל קוג'מן (Yuval Kogman)
104              
105             =head1 COPYRIGHT AND LICENCE
106              
107             This software is copyright (c) 2008 by Yuval Kogman.
108              
109             This is free software; you can redistribute it and/or modify it under
110             the same terms as the Perl 5 programming language system itself.
111              
112             =cut
113              
114             __END__