File Coverage

blib/lib/Pod/Perldoc/Cache.pm
Criterion Covered Total %
statement 54 67 80.6
branch 8 18 44.4
condition 2 3 66.6
subroutine 12 14 85.7
pod 2 3 66.6
total 78 105 74.2


line stmt bran cond sub pod time code
1             package Pod::Perldoc::Cache;
2 4     4   69494 use 5.008005;
  4         14  
  4         146  
3 4     4   21 use strict;
  4         6  
  4         129  
4 4     4   31 use warnings;
  4         7  
  4         135  
5 4     4   3685 use File::Spec::Functions qw(catfile catdir);
  4         3754  
  4         346  
6 4     4   23 use File::Path qw(mkpath);
  4         8  
  4         260  
7 4     4   20 use Digest::MD5 qw(md5_hex);
  4         8  
  4         201  
8 4     4   5665 use Pod::Text ();
  4         241008  
  4         203  
9 4     4   306 use constant DEFAULT_PARSER_CLASS => 'Pod::Text';
  4         10  
  4         3567  
10              
11             our @ISA = ('Pod::Text');
12              
13             our $VERSION = "0.02";
14              
15             sub parse_from_file {
16 4     4 1 1334 my ($self, $pod_file, $out_fh) = @_;
17 4         5 my $parser_class = do {
18 4 50       16 if (exists $self->{_parser_class}) {
19 0         0 $self->{_parser_class};
20             } else {
21 4         8 DEFAULT_PARSER_CLASS;
22             }
23             };
24              
25 4         12 my $cache_dir = _cache_dir($ENV{POD_PERLDOC_CACHE_DIR});
26 4         11 my $cache_file = _cache_file($cache_dir, $pod_file, $parser_class);
27              
28 4 100 66     105 if (-f $cache_file && not $self->{_ignore_cache}) {
29 1 50       32 open my $cache_fh, '<', $cache_file
30             or die "Can't open $cache_file: $!";
31 1         36 print $out_fh $_ while <$cache_fh>;
32             } else {
33 3         22 my $parser = $parser_class->new;
34 3         362 $parser->parse_from_file($pod_file, $out_fh);
35              
36 3 50       3040 open my $cache_fh, '>', $cache_file
37             or die "Can't write formatted pod to $cache_file\n";
38 3         18 seek $out_fh, 0, 0;
39 3         204 print $cache_fh $_ while <$out_fh>;
40             }
41             }
42              
43             sub _cache_file {
44 7     7   10072 my ($cache_dir, $file_path, $parser_class) = @_;
45              
46 7         24 $parser_class =~ s/::/_/g;
47 7         21 my $digest = _calc_pod_md5($file_path);
48 7         23 my $suffix = ".$parser_class.$digest";
49              
50 7         26 $file_path =~ s!/!_!g;
51 7         47 return catfile($cache_dir, $file_path) . $suffix;
52             }
53              
54             sub _cache_dir {
55 5     5   783 my $cache_dir = shift;
56 5 50       17 unless ($cache_dir) {
57 0         0 $cache_dir = catdir($ENV{HOME}, '.pod_perldoc_cache');
58             }
59 5 50       91 unless (-e $cache_dir) {
60 0 0       0 mkpath $cache_dir
61             or die "Can't create cache directory: $cache_dir";
62             }
63              
64 5         15 return $cache_dir;
65             }
66              
67             sub _calc_pod_md5 {
68 8     8   67 my $pod_file = shift;
69 8         12 my $pod = do {
70 8         26 local $/;
71 8 50       415 open my $pod_fh, '<', $pod_file
72             or die "Can't read pod file: $!";
73 8         275 <$pod_fh>;
74             };
75 8         51 return md5_hex($pod);
76             }
77              
78             # called by -w option
79             sub parser {
80 0     0 0   my ($self, $parser_class) = @_;
81              
82 0           my $parser_file = $parser_class;
83 0           $parser_file =~ s!\::!/!g;
84 0           eval {
85 0           require "$parser_file.pm";
86             };
87 0 0         if ($@) {
88 0           die $@;
89             } else {
90 0           $self->{_parser_class} = $parser_class;
91             }
92             }
93              
94             # called by -w option
95             sub ignore {
96 0     0 1   my $self = shift;
97 0           $self->{_ignore_cache} = 1;
98             }
99              
100             1;
101             __END__