| 1 |
#!/usr/bin/env perl |
| 2 |
# podgrep -- grep in pod sections only |
| 3 |
|
| 4 |
# ------ pragmas |
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
use Getopt::Std qw(getopts); |
| 8 |
|
| 9 |
our $VERSION = '2.2.0'; |
| 10 |
|
| 11 |
# ------ define variables |
| 12 |
my $chunk = undef; # chunk count |
| 13 |
my $file = undef; # file name |
| 14 |
my $inmatch = undef; # TRUE if inside match |
| 15 |
my $inpod = undef; # TRUE if inside POD |
| 16 |
my $only_header = undef; # copy of $opt_h |
| 17 |
our $opt_f = undef; # output plain text, paged if -p |
| 18 |
our $opt_h = undef; # output only POD headers |
| 19 |
our $opt_i = undef; # case-insensitive pattern |
| 20 |
our $opt_p = undef; # use $ENV{PAGER} to page output |
| 21 |
my $orig_pattern = undef; # original pattern to search for |
| 22 |
my $pager = undef; # contents of $ENV{PAGER} |
| 23 |
my $pattern = undef; # pattern to search for |
| 24 |
|
| 25 |
|
| 26 |
getopts("fhpi") |
| 27 |
|| die "usage: $0 [-i] [-f] [-h] [-p] pattern [podfiles ...]"; |
| 28 |
|
| 29 |
$/ = ''; |
| 30 |
$only_header = $opt_h; |
| 31 |
$orig_pattern = $pattern = shift; |
| 32 |
$pattern = '^=.*' . $pattern if $only_header; |
| 33 |
$pattern .= '(?i)' if $opt_i; |
| 34 |
|
| 35 |
if ($opt_p) { |
| 36 |
unless ($pager = $ENV{PAGER}) { |
| 37 |
require Config; |
| 38 |
{ |
| 39 |
no warnings qw(once); |
| 40 |
$pager = $Config::Config{"pager"} || "more"; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if ($opt_f) { |
| 46 |
if ($opt_p) { |
| 47 |
open(STDOUT, "| pod2text | $pager '+/$orig_pattern'"); |
| 48 |
} else { |
| 49 |
open(STDOUT, "| pod2text"); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
elsif ($opt_p) { |
| 54 |
open(STDOUT, "| $pager '+/$orig_pattern'"); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
($file, $chunk) = ('-', 0); |
| 59 |
|
| 60 |
while (<>) { |
| 61 |
if ($inpod && /^=cut/) { |
| 62 |
$inmatch = $inpod = 0; |
| 63 |
next; |
| 64 |
} |
| 65 |
|
| 66 |
if (! $inpod && /^=(?!cut)\w+/) { |
| 67 |
$inpod = 1; |
| 68 |
} |
| 69 |
|
| 70 |
if ($inmatch && /^=\w+/) { |
| 71 |
$inmatch = 0; |
| 72 |
} |
| 73 |
|
| 74 |
if ($inpod && !$inmatch && /$pattern/o) { |
| 75 |
print "=head1 $ARGV chunk $.\n\n" |
| 76 |
unless $file eq $ARGV && $chunk+1 == $.; |
| 77 |
($file, $chunk) = ($ARGV, $.); |
| 78 |
print; |
| 79 |
$inmatch = 1 if $only_header; |
| 80 |
next; |
| 81 |
} |
| 82 |
|
| 83 |
print if $inmatch; |
| 84 |
|
| 85 |
|
| 86 |
} continue { |
| 87 |
|
| 88 |
if (eof) { |
| 89 |
$inmatch = $inpod = 0; |
| 90 |
($file, $chunk) = ('-', 0); |
| 91 |
close ARGV; |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
close STDOUT; |
| 97 |
|
| 98 |
__END__ |
| 99 |
|
| 100 |
=head1 NAME |
| 101 |
|
| 102 |
podgrep - grep in pod sections only |
| 103 |
|
| 104 |
=head1 SYNOPSIS |
| 105 |
|
| 106 |
podgrep [B<-i>] [B<-p>] [B<-f>] [B<-h>] I<pattern> [ I<files> ... ] |
| 107 |
|
| 108 |
=head1 DESCRIPTION |
| 109 |
|
| 110 |
This program searches each paragraph in a pod document and prints each |
| 111 |
paragraph that matches the supplied pattern. This pod may be mixed with |
| 112 |
program code, such as in a module. |
| 113 |
|
| 114 |
Options are: |
| 115 |
|
| 116 |
=over 4 |
| 117 |
|
| 118 |
=item -i |
| 119 |
|
| 120 |
means case insensitive match |
| 121 |
|
| 122 |
=item -p |
| 123 |
|
| 124 |
means page output though the user's pager. The pager will be primed |
| 125 |
with an argument to search for the string. This highlights the result. |
| 126 |
|
| 127 |
=item -f |
| 128 |
|
| 129 |
means format output though the I<pod2text> program. |
| 130 |
|
| 131 |
=item -h |
| 132 |
|
| 133 |
means check for matches in pod C<=head> and C<=item> headers alone, |
| 134 |
and to keep printing podagraphs until the next header is found. |
| 135 |
|
| 136 |
=back |
| 137 |
|
| 138 |
|
| 139 |
=head1 EXAMPLES |
| 140 |
|
| 141 |
$ podgrep mail `pmpath CGI` |
| 142 |
(prints out podagraphs from the CGI.pm manpage that mention mail) |
| 143 |
|
| 144 |
$ podgrep -i destructor `sitepods` |
| 145 |
(prints out podagraphs that mention destructors in the |
| 146 |
site-installed pods) |
| 147 |
|
| 148 |
$ podgrep -i 'type.?glob' `stdpods` |
| 149 |
|
| 150 |
(prints out podagraphs that mention typeglob in the |
| 151 |
standard pods) |
| 152 |
|
| 153 |
$ podgrep -hpfi "lock" `faqpods` |
| 154 |
|
| 155 |
(prints out all podagraphs with "lock" in the headers |
| 156 |
case-insensitively, then formats these with pod2text, then |
| 157 |
shows them in the pager with matches high-lighted) |
| 158 |
|
| 159 |
$ podgrep -fh seek `podpath perlfunc` |
| 160 |
(prints out and formats podagraphs from the standard perlfunc manpage |
| 161 |
whose headers or items contain "seek".) |
| 162 |
|
| 163 |
=head1 SEE ALSO |
| 164 |
|
| 165 |
faqpods(1), |
| 166 |
pfcat(1), |
| 167 |
pmpath(1), |
| 168 |
pod2text(1), |
| 169 |
podpath(1), |
| 170 |
sitepods(1), |
| 171 |
stdpods(1), |
| 172 |
and |
| 173 |
tcgrep(1). |
| 174 |
|
| 175 |
=head1 NOTE |
| 176 |
|
| 177 |
For a pager, the author likes these environment settings (in the login |
| 178 |
startup, of course): |
| 179 |
|
| 180 |
$ENV{PAGER} = "less"; |
| 181 |
$ENV{LESS} = "MQeicsnf"; |
| 182 |
|
| 183 |
=head1 AUTHORS and COPYRIGHTS |
| 184 |
|
| 185 |
Copyright (C) 1999 Tom Christiansen. |
| 186 |
|
| 187 |
Copyright (C) 2006-2014, 2018 Mark Leighton Fisher. |
| 188 |
|
| 189 |
=head1 LICENSE |
| 190 |
|
| 191 |
This is free software; you can redistribute it and/or modify it |
| 192 |
under the terms of either: |
| 193 |
(a) the GNU General Public License as published by the Free |
| 194 |
Software Foundation; either version 1, or (at your option) any |
| 195 |
later version, or |
| 196 |
(b) the Perl "Artistic License". |
| 197 |
(This is the Perl 5 licensing scheme.) |
| 198 |
|
| 199 |
Please note this is a change from the |
| 200 |
original pmtools-1.00 (still available on CPAN), |
| 201 |
as pmtools-1.00 were licensed only under the |
| 202 |
Perl "Artistic License". |