File Coverage

blib/lib/Pod/Sub/Usage.pm
Criterion Covered Total %
statement 36 36 100.0
branch 9 12 75.0
condition 5 9 55.5
subroutine 7 7 100.0
pod 2 2 100.0
total 59 66 89.3


line stmt bran cond sub pod time code
1             package Pod::Sub::Usage;
2              
3 4     4   41215 use 5.006;
  4         15  
4 4     4   19 use strict;
  4         8  
  4         74  
5 4     4   20 use warnings;
  4         11  
  4         167  
6              
7             $Pod::Sub::Usage::VERSION = '0.010000';
8              
9 4     4   23 use Exporter 'import';
  4         8  
  4         1292  
10             our @EXPORT_OK = qw( sub2usage pod_text);
11             our %EXPORT_TAGS = ( all => [@EXPORT_OK], );
12              
13             =head1 NAME
14              
15             Pod::Sub::Usage - Module to print sub documentaion from pod!
16              
17             =head1 VERSION
18              
19             Version 0.010000
20              
21             =head1 SYNOPSIS
22              
23             use Pod::Sub::Usage qw/sub2usage/;
24              
25             # print header from 'your_sub' in current package
26             sub2usage('your_sub');
27              
28             # same here
29             sub2usage('your_sub', __PACKAGE__);
30              
31             # print header from 'your_sub' in some other package
32             sub2usage('your_sub', 'Use::Some::Package' );
33              
34             =head1 EXPORT
35              
36             Nothing is exported by default. You can ask for specific subroutines (described below) or ask for all subroutines at once:
37              
38             use Pod::Sub::Usage qw/sub2usage/;
39            
40             # or
41            
42             use Pod::Sub::Usage qw/all/;
43              
44             =head1 SUBROUTINES/METHODS
45              
46             =head2 sub2usage
47              
48             Print out the header information by given sub.
49              
50             =cut
51              
52             sub sub2usage {
53 3     3 1 431 my ( $sub, $package ) = @_;
54 3 50       12 die q~You have to say the sub if you want to know something about it!~ if !$sub;
55 3         1535 require Module::Locate;
56 3   33     29583 my $file = Module::Locate::locate( $package ||= ( caller(0) )[0] ) // ( caller(0) )[1];
      66        
57 3         456 my $string = pod_text( $file, $package, $sub );
58 3         371 print $string;
59             }
60              
61             =head2 pod_text
62              
63             Returns the string from pod
64              
65             =cut
66              
67             sub pod_text {
68 6     6 1 23 my ( $file, $package, $sub ) = @_;
69 6 50   3   164 open( my $fh, '<:encoding(UTF-8)', $file ) or die "Could not open file '$file' $!";
  3         19  
  3         6  
  3         21  
70 6         27223 my $rex_start_head = qr/^=head\d ($sub|$package::$sub)/;
71 6         18 my $found = 0;
72 6         14 my $sub_header = '';
73 6         100 while ( my $row = <$fh> ) {
74 168 100 66     602 last if ( $row =~ /^=cut/ && $found );
75 162 100       535 if ( $row =~ /$rex_start_head/ ) {
76 6         13 $found = 1;
77 6         26 next;
78             }
79 156 100       580 if ($found) {
80 18         45 chomp $row;
81 18         40 $row =~ s/^=head\d\s+//;
82 18         89 $sub_header .= "$row\n";
83             }
84             }
85 6 50       18 die qq~Couldn't find $sub in file $file. $!~ if !$found;
86 6         94 return $sub_header;
87             }
88              
89             =head1 AUTHOR
90              
91             Mario Zieschang, C<< >>
92              
93             =head1 BUGS
94              
95             Please report any bugs or feature requests to C, or through
96             the web interface at L. I will be notified, and then you'll
97             automatically be notified of progress on your bug as I make changes.
98              
99              
100              
101             =head1 SUPPORT
102              
103             You can find documentation for this module with the perldoc command.
104              
105             perldoc Pod::Sub::Usage
106              
107              
108             You can also look for information at:
109              
110             =over 4
111              
112             =item * RT: CPAN's request tracker (report bugs here)
113              
114             L
115              
116             =item * AnnoCPAN: Annotated CPAN documentation
117              
118             L
119              
120             =item * CPAN Ratings
121              
122             L
123              
124             =item * Search CPAN
125              
126             L
127              
128             =back
129              
130             =head1 SEE ALSO
131            
132             This package was partly inspired by on L by Marek Rouchal.
133              
134              
135             =head1 LICENSE AND COPYRIGHT
136              
137             Copyright 2017 Mario Zieschang.
138              
139             This program is free software; you can redistribute it and/or modify it
140             under the terms of the the Artistic License (2.0). You may obtain a
141             copy of the full license at:
142              
143             L
144              
145             Any use, modification, and distribution of the Standard or Modified
146             Versions is governed by this Artistic License. By using, modifying or
147             distributing the Package, you accept this license. Do not use, modify,
148             or distribute the Package, if you do not accept this license.
149              
150             If your Modified Version has been derived from a Modified Version made
151             by someone other than you, you are nevertheless required to ensure that
152             your Modified Version complies with the requirements of this license.
153              
154             This license does not grant you the right to use any trademark, service
155             mark, tradename, or logo of the Copyright Holder.
156              
157             This license includes the non-exclusive, worldwide, free-of-charge
158             patent license to make, have made, use, offer to sell, sell, import and
159             otherwise transfer the Package with respect to any patent claims
160             licensable by the Copyright Holder that are necessarily infringed by the
161             Package. If you institute patent litigation (including a cross-claim or
162             counterclaim) against any party alleging that the Package constitutes
163             direct or contributory patent infringement, then this Artistic License
164             to you shall terminate on the date that such litigation is filed.
165              
166             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
167             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
168             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
169             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
170             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
171             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
172             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
173             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
174              
175              
176             =cut
177              
178             1; # End of Pod::Sub::Usage