File Coverage

blib/lib/Pod/Sub/Usage.pm
Criterion Covered Total %
statement 42 42 100.0
branch 7 8 87.5
condition 9 17 52.9
subroutine 9 9 100.0
pod 2 2 100.0
total 69 78 88.4


line stmt bran cond sub pod time code
1             package Pod::Sub::Usage;
2              
3 4     4   138527 use Carp;
  4         19  
  4         152  
4 4     4   66 use 5.010;
  4         8  
5 4     4   13 use strict;
  4         5  
  4         69  
6 4     4   14 use warnings;
  4         4  
  4         148  
7              
8             $Pod::Sub::Usage::VERSION = '0.02000';
9              
10 4     4   28 use Exporter 'import';
  4         6  
  4         1327  
11             our @EXPORT_OK = qw( sub2usage pod_text);
12             our %EXPORT_TAGS = (all => [@EXPORT_OK],);
13              
14             sub sub2usage {
15 3     3 1 1233 my ($package, $sub) = @_;
16 3         1055 require Module::Locate;
17 3   33     26020 my $file = Module::Locate::locate($package ||= (caller 0)[0]) // (caller 0)[1];
      66        
18 3         357 return print pod_text($file, $package, $sub);
19             }
20              
21             sub pod_text {
22 6     6 1 9 my $file = shift;
23 6         10 my $package = shift;
24 6   50     11 my $sub = shift // 'SYNOPSIS';
25 6   33 3   153 open my $fh, '<:encoding(UTF-8)', $file || croak qq~Could not open: '$file'~;
  3         14  
  3         3  
  3         15  
26 6         25776 my $sub_header = _parse_file($fh, qr/^=(cut|head\d|over)\s+($sub|$package::$sub)/sxm);
27 6         93 my $is_closed = close $fh;
28 6 50 33     42 croak qq~Couldn't find '$sub' in: $file~ if !$sub_header || !$is_closed;
29 6         125 return $sub_header;
30             }
31              
32             sub _parse_file {
33 6     6   18 my ($fh, $rex_start_head) = @_;
34 6         8 my $found = 0;
35 6         7 my $sub_header = q//;
36 6         120 while (my $row = <$fh>) {
37 196 100 100     469 last if ($row =~ /^=(cut|head|over)/sxm && $found);
38 190 100       526 if ($row =~ /$rex_start_head/sxm) {
39 6         23 $found = 1;
40 6         18 next;
41             }
42 184 100       463 if ($found) {
43 18         57 chomp $row;
44 18         40 $row =~ s/^=head\d\s+//sxm;
45 18         81 $sub_header .= "$row\n";
46             }
47             }
48 6         18 return $sub_header;
49             }
50              
51             1;
52              
53             __END__