File Coverage

blib/lib/Poet/Tools.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             # Internal Poet tools
2             #
3             package Poet::Tools;
4             $Poet::Tools::VERSION = '0.16';
5 3     3   14 use Carp;
  3         4  
  3         224  
6 3     3   1635 use Class::Load;
  3         79720  
  3         114  
7 3     3   3098 use Class::MOP;
  0            
  0            
8             use Config;
9             use Fcntl qw( :DEFAULT :seek );
10             use File::Basename;
11             use File::Find;
12             use File::Path;
13             use File::Slurp qw(read_dir);
14             use File::Spec::Functions ();
15             use File::Temp qw(tempdir);
16             use Try::Tiny;
17             use strict;
18             use warnings;
19             use base qw(Exporter);
20              
21             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             my $Fetch_Flags = O_RDONLY | O_BINARY;
25             my $Store_Flags = O_WRONLY | O_CREAT | O_BINARY;
26             my $File_Spec_Using_Unix = $File::Spec::ISA[0] eq 'File::Spec::Unix';
27              
28             sub can_load {
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             my ($class_name) = @_;
34              
35             my $result;
36             try {
37             Class::Load::load_class($class_name);
38             $result = 1;
39             }
40             catch {
41             if ( /Can\'t locate .* in \@INC/ && !/Compilation failed/ ) {
42             $result = 0;
43             }
44             else {
45             die $_;
46             }
47             };
48             return $result;
49             }
50              
51             sub catdir {
52             return $File_Spec_Using_Unix
53             ? join( "/", @_ )
54             : File::Spec::Functions::catdir(@_);
55             }
56              
57             sub catfile {
58             return $File_Spec_Using_Unix
59             ? join( "/", @_ )
60             : File::Spec::Functions::catfile(@_);
61             }
62              
63             sub checksum {
64             my ($str) = @_;
65              
66             # Adler32 algorithm
67             my $s1 = 1;
68             my $s2 = 1;
69             for my $c ( unpack( "C*", $str ) ) {
70             $s1 = ( $s1 + $c ) % 65521;
71             $s2 = ( $s2 + $s1 ) % 65521;
72             }
73             return ( $s2 << 16 ) + $s1;
74             }
75              
76             # From File::Find::Wanted
77             sub find_wanted {
78             my $func = shift;
79             my @files;
80              
81             local $_;
82             find( sub { push @files, $File::Find::name if &$func }, @_ );
83              
84             return @files;
85             }
86              
87             # Return perl executable - from ExtUtils::MM_Unix
88             sub perl_executable {
89             my $interpreter;
90             if ( $Config{startperl} =~ m,^\#!.*/perl, ) {
91             $interpreter = $Config{startperl};
92             $interpreter =~ s,^\#!,,;
93             }
94             else {
95             $interpreter = $Config{perlpath};
96             }
97             return $interpreter;
98             }
99              
100             sub read_file {
101             my ($file) = @_;
102              
103             # Fast slurp, adapted from File::Slurp::read, with unnecessary options removed
104             #
105             my $buf = "";
106             my $read_fh;
107             unless ( sysopen( $read_fh, $file, $Fetch_Flags ) ) {
108             croak "read_file '$file' - sysopen: $!";
109             }
110             my $size_left = -s $read_fh;
111             while (1) {
112             my $read_cnt = sysread( $read_fh, $buf, $size_left, length $buf );
113             if ( defined $read_cnt ) {
114             last if $read_cnt == 0;
115             $size_left -= $read_cnt;
116             last if $size_left <= 0;
117             }
118             else {
119             croak "read_file '$file' - sysread: $!";
120             }
121             }
122             return $buf;
123             }
124              
125             sub tempdir_simple {
126             my ($template) = @_;
127              
128             return tempdir( $template, TMPDIR => 1, CLEANUP => 1 );
129             }
130              
131             sub trim {
132             my ($str) = @_;
133             if ( defined($str) ) {
134             for ($str) { s/^\s+//; s/\s+$// }
135             }
136             return $str;
137             }
138              
139             # From List::MoreUtils
140             sub uniq (@) {
141             my %h;
142             map { $h{$_}++ == 0 ? $_ : () } @_;
143             }
144              
145             sub taint_is_on {
146             return ${^TAINT} ? 1 : 0;
147             }
148              
149             sub write_file {
150             my ( $file, $data, $file_create_mode ) = @_;
151              
152             ($file) = $file =~ /^(.*)/s if taint_is_on(); # Untaint blindly
153             $file_create_mode = oct(666) if !defined($file_create_mode);
154              
155             # Fast spew, adapted from File::Slurp::write, with unnecessary options removed
156             #
157             {
158             my $write_fh;
159             unless ( sysopen( $write_fh, $file, $Store_Flags, $file_create_mode ) ) {
160             croak "write_file '$file' - sysopen: $!";
161             }
162             my $size_left = length($data);
163             my $offset = 0;
164             do {
165             my $write_cnt = syswrite( $write_fh, $data, $size_left, $offset );
166             unless ( defined $write_cnt ) {
167             croak "write_file '$file' - syswrite: $!";
168             }
169             $size_left -= $write_cnt;
170             $offset += $write_cnt;
171             } while ( $size_left > 0 );
172             truncate( $write_fh, sysseek( $write_fh, 0, SEEK_CUR ) )
173             }
174             }
175              
176             1;