PPI/Util.pm

filter: Statementstmt 100% 45 / 45 Branchbran 72.2% 13 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 2 totaltotal 90.7% 69 / 76 CC 10 SCAR 12.6
CC
10
Cov
92%
_Document
7.0
_slurp
3.1
md5hex_file
2.0
CRAP 3.5
« PPI/Statement/Sub.pm ↑ Summary PPI/Token/Number/Binary.pm »
1 package PPI::Util;
2  
3 # Provides some common utility functions that can be imported
4  
5 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 62 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 62 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 62 ▶use Params::Util qw{_INSTANCE _SCALAR0 _ARRAY0};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 62 ▶use vars qw{$VERSION @ISA @EXPORT_OK};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 BEGIN {
12 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 62 @ISA = 'Exporter';
14 62 @EXPORT_OK = qw{_Document _slurp};
15 }
16  
17 # 5.8.7 was the first version to resolve the notorious
18 # "unicode length caching" bug.
19 62 ▶use constant HAVE_UNICODE => !! ( $] >= 5.008007 );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Common reusable true and false functions
22 # This makes it easy to upgrade many places in PPI::XS
23 sub TRUE () { 1 }
24 sub FALSE () { '' }
25  
26  
27  
28  
29  
30 #####################################################################
31 # Functions
32  
33 # Allows a sub that takes a L<PPI::Document> to handle the full range
34 # of different things, including file names, SCALAR source, etc.
35 sub _Document {
36 17 ▶ shift if @_ > 1;
Subroutine _Document: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 17 17
37 17 ▶ return undef unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 17 17
38 17 require PPI::Document;
39 17 ▶ return PPI::Document->new(shift) unless ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 1 16 17
40 16 ▶ return PPI::Document->new(shift) if _SCALAR0($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &_SCALAR0($_[0]) 6 10 16
41 10 ▶ return PPI::Document->new(shift) if _ARRAY0($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &_ARRAY0($_[0]) 1 9 10
42 9 ▶ return shift if _INSTANCE($_[0], 'PPI::Document');
Branch: 1 branch
BranchTrueFalseTotal
if &_INSTANCE($_[0], "PPI::Document") 7 2 9
43 2 return undef;
44 }
45  
46 # Provide a simple _slurp implementation
47 sub _slurp {
48 625 ▶ my $file = shift;
Subroutine _slurp: called CC=3 SCAR=11.2
49 625 local $/ = undef;
50 625 local *FILE;
51 625 ▶ open( FILE, '<', $file ) or return "open($file) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $file or return "open($file) failed: $!" 0 625 625
52 625 my $source = <FILE>;
53 625 ▶ close( FILE ) or return "close($file) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
close FILE or return "close($file) failed: $!" 0 625 625
54 625 return \$source;
55 }
56  
57 # Provides a version of Digest::MD5's md5hex that explicitly
58 # works on the unix-newlined version of the content.
59 sub md5hex {
60 315 ▶ my $string = shift;
Subroutine md5hex: called CC=1 SCAR=0.0
Pod undocumented
61 315 $string =~ s/(?:\015{1,2}\012|\015|\012)/\015/gs;
62 315 Digest::MD5::md5_hex($string);
63 }
64  
65 # As above but slurps and calculates the id for a file by name
66 sub md5hex_file {
67 153 ▶ my $file = shift;
Subroutine md5hex_file: called CC=2 SCAR=7.0
Pod undocumented
68 153 my $content = _slurp($file);
69 153 ▶ return undef unless ref $content;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content 0 153 153
70 153 $$content =~ s/(?:\015{1,2}\012|\015|\012)/\n/gs;
71 153 md5hex($$content);
72 }
73  
74 62 1;
« PPI/Statement/Sub.pm ↑ Summary PPI/Token/Number/Binary.pm »