3 # Provides some common utility functions that can be imported
8 use Params::Util qw{_INSTANCE _SCALAR0 _ARRAY0};
10 use vars qw{$VERSION @ISA @EXPORT_OK};
14 @EXPORT_OK = qw{_Document _slurp};
17 # Alarms are used to catch unexpectedly slow and complex documents
18 use constant HAVE_ALARM => ! ( $^O eq 'MSWin32' or $^O eq 'cygwin' );
20 # 5.8.7 was the first version to resolve the notorious
21 # "unicode length caching" bug. See RT #FIXME
22 use constant HAVE_UNICODE => !! ( $] >= 5.008007 );
24 # Common reusable true and false functions
25 # This makes it easy to upgrade many places in PPI::XS
33 #####################################################################
36 # Allows a sub that takes a L<PPI::Document> to handle the full range
37 # of different things, including file names, SCALAR source, etc.
40 return undef unless defined $_[0];
41 require PPI::Document;
42 return PPI::Document->new(shift) unless ref $_[0];
43 return PPI::Document->new(shift) if _SCALAR0($_[0]);
44 return PPI::Document->new(shift) if _ARRAY0($_[0]);
45 return shift if _INSTANCE($_[0], 'PPI::Document');
49 # Provide a simple _slurp implementation
54 open( FILE, '<', $file ) or return "open($file) failed: $!";
56 close( FILE ) or return "close($file) failed: $!";
60 # Provides a version of Digest::MD5's md5hex that explicitly
61 # works on the unix-newlined version of the content.
64 $string =~ s/(?:\015{1,2}\012|\015|\012)/\015/gs;
65 Digest::MD5::md5_hex($string);
68 # As above but slurps and calculates the id for a file by name
71 my $content = _slurp($file);
72 return undef unless ref $content;
73 $$content =~ s/(?:\015{1,2}\012|\015|\012)/\n/gs;