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