blib/lib/Poet/Tools.pm

filter: Statementstmt 81.3% 100 / 123 Branchbran 44.1% 15 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 25 / 30 Podpod 0.0% 0 / 12 totaltotal 69.1% 141 / 204 CC 21 SCAR 12.2
CC
21
Cov
72%
write_file
6.7
catfile
6.0
checksum
6.0
CRAP 3.4
« blib/lib/Poet/t/Run.pm ↑ Summary blib/lib/Poet/Conf.pm »
1 # Internal Poet tools
2 #
3 package Poet::Tools;
4 12 $Poet::Tools::VERSION = '0.16';
5 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Fcntl qw( :DEFAULT :seek );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use File::Slurp qw(read_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use File::Spec::Functions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 12 our @EXPORT_OK =
22 qw(basename can_load catdir catfile checksum dirname find_wanted mkpath perl_executable read_dir read_file rmtree taint_is_on tempdir_simple trim uniq write_file );
23  
24 12 my $Fetch_Flags = O_RDONLY | O_BINARY;
25 12 my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
26 12 my $File_Spec_Using_Unix = $File::Spec::ISA[0] eq 'File::Spec::Unix';
27  
28 422 ▶sub can_load {
Subroutine can_load: called CC=1 SCAR=0.2
Pod undocumented
29  
30 # Load $class_name if possible. Return 1 if successful, 0 if it could not be
31 # found, and rethrow load error (other than not found).
32 #
33 422 my ($class_name) = @_;
34  
35 422 my $result;
36 422 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
37 422 Class::Load::load_class($class_name);
38 253 $result = 1;
39 }
40 169 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=14.2
41 169 ▶ if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/Can\'t locate .* in \@INC/ and not /Compilation failed/) { } 169 0 169
Condition33%1/3
/Can\'t locate .* in \@INC/ and not /Compilation failed/
!ll&&!rl&&r
00169
MC/DC0%0/2
/Can\'t locate .* in \@INC/ and not /Compilation failed/
/Can\'t locate .* in \@INC/ not /Compilation failed/
Truth table33%1/3
A and not(B)
A= /Can\'t locate .* in \@INC/B= not /Compilation failed/
ABresult
0X0
100
111
42 169 $result = 0;
43 }
44 else {
45 0 die $_;
46 }
47 422 };
48 422 return $result;
49 }
50  
51 19 ▶sub catdir {
Subroutine catdir: called CC=2 SCAR=7.6
Pod undocumented
52 19 ▶ return $File_Spec_Using_Unix
Branch: 1 branch
BranchTrueFalseTotal
$File_Spec_Using_Unix ? : 19 0 19
53 ? join( "/", @_ )
54 : File::Spec::Functions::catdir(@_);
55 }
56  
57 0 ▶sub catfile {
Subroutine catfile: not called CC=2 SCAR=17.9
Pod undocumented
58 0 ▶ return $File_Spec_Using_Unix
Branch: 1 branch
BranchTrueFalseTotal
$File_Spec_Using_Unix ? : 0 0 0
59 ? join( "/", @_ )
60 : File::Spec::Functions::catfile(@_);
61 }
62  
63 0 ▶sub checksum {
Subroutine checksum: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my ($str) = @_;
65  
66 # Adler32 algorithm
67 0 my $s1 = 1;
68 0 my $s2 = 1;
69 0 for my $c ( unpack( "C*", $str ) ) {
70 0 $s1 = ( $s1 + $c ) % 65521;
71 0 $s2 = ( $s2 + $s1 ) % 65521;
72 }
73 0 return ( $s2 << 16 ) + $s1;
74 }
75  
76 # From File::Find::Wanted
77 0 ▶sub find_wanted {
Subroutine find_wanted: not called CC=1 SCAR=6.9
Pod undocumented
78 0 my $func = shift;
79 0 my @files;
80  
81 0 local $_;
82 0 ▶ find( sub { push @files, $File::Find::name if &$func }, @_ );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if &$func 0 0 0
83  
84 0 return @files;
85 }
86  
87 # Return perl executable - from ExtUtils::MM_Unix
88 55 ▶sub perl_executable {
Subroutine perl_executable: called CC=2 SCAR=7.2
Pod undocumented
89 55 my $interpreter;
90 55 ▶ if ( $Config{startperl} =~ m,^\#!.*/perl, ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Poet::Tools::Config{'startperl'} =~ m[^\#!.*/perl]) { } 55 0 55
91 55 $interpreter = $Config{startperl};
92 55 $interpreter =~ s,^\#!,,;
93 }
94 else {
95 0 $interpreter = $Config{perlpath};
96 }
97 55 return $interpreter;
98 }
99  
100 83 ▶sub read_file {
Subroutine read_file: called CC=5 SCAR=16.7
Pod undocumented
101 83 my ($file) = @_;
102  
103 # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
104 #
105 83 my $buf = "";
106 83 my $read_fh;
107 83 ▶ unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $read_fh, $file, $Fetch_Flags) 0 83 83
108 0 croak "read_file '$file' - sysopen: $!";
109 }
110 83 my $size_left = -s $read_fh;
111 83 while (1) {
112 83 my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf );
113 83 ▶ if ( defined $read_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_cnt) { } 83 0 83
114 83 ▶ last if $read_cnt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $read_cnt == 0 1 82 83
115 82 $size_left -= $read_cnt;
116 82 ▶ last if $size_left <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size_left <= 0 82 0 82
117 }
118 else {
119 0 croak "read_file '$file' - sysread: $!";
120 }
121 }
122 83 return $buf;
123 }
124  
125 21 ▶sub tempdir_simple {
Subroutine tempdir_simple: called CC=1 SCAR=0.0
Pod undocumented
126 21 my ($template) = @_;
127  
128 21 return tempdir( $template, TMPDIR => 1, CLEANUP => 1 );
129 }
130  
131 579 ▶sub trim {
Subroutine trim: called CC=3 SCAR=11.0
Pod undocumented
132 579 my ($str) = @_;
133 579 ▶ if ( defined($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 579 0 579
134 579 for ($str) { s/^\s+//; s/\s+$// }
135 }
136 579 return $str;
137 }
138  
139 # From List::MoreUtils
140 0 ▶sub uniq (@) {
Subroutine uniq: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my %h;
142 0 ▶ map { $h{$_}++ == 0 ? $_ : () } @_;
Branch: 1 branch
BranchTrueFalseTotal
$h{$_}++ == 0 ? : 0 0 0
143 }
144  
145 464 ▶sub taint_is_on {
Subroutine taint_is_on: called CC=2 SCAR=7.6
Pod undocumented
146 464 ▶ return ${^TAINT} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
${^TAINT} ? : 0 464 464
147 }
148  
149 464 ▶sub write_file {
Subroutine write_file: called CC=6 SCAR=19.0
Pod undocumented
150 464 my ( $file, $data, $file_create_mode ) = @_;
151  
152 464 ▶ ($file) = $file =~ /^(.*)/s if taint_is_on(); # Untaint blindly
Branch: 1 branch
BranchTrueFalseTotal
if taint_is_on() 0 464 464
153 464 ▶ $file_create_mode = oct(666) if !defined($file_create_mode);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_create_mode 464 0 464
154  
155 # Fast spew, adapted from File::Slurp::write, with unnecessary options removed
156 #
157 {
158 464 my $write_fh;
159 464 ▶ unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $write_fh, $file, $Store_Flags, $file_create_mode) 0 464 464
160 0 croak "write_file '$file' - sysopen: $!";
161 }
162 464 my $size_left = length($data);
163 464 my $offset = 0;
164 464 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$size_left > 0 and do { do { my $write_cnt = syswrite($write_fh, $data, $size_left, $offset); unless (defined $write_cnt) { croak("write_file '${file}' - syswrite: $!"); } ; $size_left -= $write_cnt; $offset += $write_cnt } } 0 464 464
165 464 my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset );
166 464 ▶ unless ( defined $write_cnt ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $write_cnt) 0 464 464
167 0 croak "write_file '$file' - syswrite: $!";
168 }
169 464 $size_left -= $write_cnt;
170 464 $offset += $write_cnt;
171 } while ( $size_left > 0 );
172 464 truncate( $write_fh, sysseek( $write_fh, 0, SEEK_CUR ) )
173 }
174 }
175  
176 12 1;
« blib/lib/Poet/t/Run.pm ↑ Summary blib/lib/Poet/Conf.pm »