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   186330 use Carp;
  4         23  
  4         197  
4 4     4   92 use 5.010;
  4         10  
5 4     4   25 use strict;
  4         6  
  4         87  
6 4     4   15 use warnings;
  4         11  
  4         155  
7              
8             $Pod::Sub::Usage::VERSION = '0.02001';
9              
10 4     4   24 use Exporter 'import';
  4         13  
  4         1553  
11             our @EXPORT_OK = qw( sub2usage pod_text);
12             our %EXPORT_TAGS = (all => [@EXPORT_OK],);
13              
14             sub sub2usage {
15 3     3 1 1616 my ($package, $sub) = @_;
16 3         1227 require Module::Locate;
17 3   33     31100 my $file = Module::Locate::locate($package ||= (caller 0)[0]) // (caller 0)[1];
      66        
18 3         545 return print pod_text($file, $package, $sub);
19             }
20              
21             sub pod_text {
22 6     6 1 13 my $file = shift;
23 6         10 my $package = shift;
24 6   50     15 my $sub = shift // 'SYNOPSIS';
25 6   33 3   198 open my $fh, '<:encoding(UTF-8)', $file || croak qq~Could not open: '$file'~;
  3         17  
  3         4  
  3         24  
26 6         30494 my $sub_header = _parse_file($fh, qr/^=(cut|head\d|over)\s+($sub|$package::$sub)/sxm);
27 6         131 my $is_closed = close $fh;
28 6 50 33     46 croak qq~Couldn't find '$sub' in: $file~ if !$sub_header || !$is_closed;
29 6         179 return $sub_header;
30             }
31              
32             sub _parse_file {
33 6     6   28 my ($fh, $rex_start_head) = @_;
34 6         11 my $found = 0;
35 6         10 my $sub_header = q//;
36 6         152 while (my $row = <$fh>) {
37 196 100 100     535 last if ($row =~ /^=(cut|head|over)/sxm && $found);
38 190 100       616 if ($row =~ /$rex_start_head/sxm) {
39 6         10 $found = 1;
40 6         24 next;
41             }
42 184 100       553 if ($found) {
43 18         43 chomp $row;
44 18         34 $row =~ s/^=head\d\s+//sxm;
45 18         95 $sub_header .= "$row\n";
46             }
47             }
48 6         18 return $sub_header;
49             }
50              
51             1;
52              
53             __END__