| 1 |
#!/usr/bin/perl |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
|
| 5 |
use Config; |
| 6 |
use Perldoc::Search; |
| 7 |
use Shell::POSIX::Select; |
| 8 |
use File::Basename; |
| 9 |
use Getopt::Std; |
| 10 |
use Pod::Simple::TextContent; |
| 11 |
|
| 12 |
our $LESS = "/usr/bin/less"; |
| 13 |
|
| 14 |
getopts("u", \my %opts); |
| 15 |
|
| 16 |
my $searcher = Perldoc::Search->new(); |
| 17 |
|
| 18 |
if($opts{u}) { |
| 19 |
print "This will take a couple of minutes, please stand by.\n"; |
| 20 |
$searcher->update(); |
| 21 |
print "Done\n"; |
| 22 |
} elsif(@ARGV) { |
| 23 |
search(); |
| 24 |
} |
| 25 |
|
| 26 |
########################################### |
| 27 |
sub search { |
| 28 |
########################################### |
| 29 |
|
| 30 |
# Search documents containing both "swish" and "install" |
| 31 |
my @hits = $searcher->search("@ARGV"); |
| 32 |
|
| 33 |
if ( !@hits ) { |
| 34 |
print "No Results\n"; |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
my @select = (); |
| 39 |
my %map = (); |
| 40 |
|
| 41 |
# Shut up the goofy Shell::POSIX::Select implementation |
| 42 |
our($Eof, $Reply); |
| 43 |
|
| 44 |
for my $hit (@hits) { |
| 45 |
if(! defined $hit) { |
| 46 |
die "Before searching, please run the indexer: perldig -u\n"; |
| 47 |
} |
| 48 |
my $path = my $org_path = $hit->path(); |
| 49 |
$path = $searcher->relative($path); |
| 50 |
push @select, $path; |
| 51 |
$map{$path} = $org_path; |
| 52 |
} |
| 53 |
|
| 54 |
@select = sort @select; |
| 55 |
|
| 56 |
select my $file (@select) { |
| 57 |
system "$LESS $map{$file}"; |
| 58 |
last; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
__END__ |
| 63 |
|
| 64 |
=head1 NAME |
| 65 |
|
| 66 |
perldig - Dig up keywords in the local Perl documentation |
| 67 |
|
| 68 |
=head1 SYNOPSIS |
| 69 |
|
| 70 |
# Update the index (required before first start) |
| 71 |
perldig -u |
| 72 |
|
| 73 |
# Search for a keyword |
| 74 |
perldig keyword(s) |
| 75 |
|
| 76 |
=head1 DESCRIPTION |
| 77 |
|
| 78 |
When using C<perldig> for the first time, a new index needs to be |
| 79 |
created. Just call |
| 80 |
|
| 81 |
$ perldig -u |
| 82 |
|
| 83 |
and everything happens automatically: A crawler will detect locally |
| 84 |
installed Perl documentation pages, rummage through the POD and index |
| 85 |
them. When this initial run has been completed, C<perldig> is ready to |
| 86 |
process search requests: |
| 87 |
|
| 88 |
$ perldig frobnicate |
| 89 |
|
| 90 |
1) pod/perlguts.pod 2) pod/perlxstut.pod 3) pod/perlnewmod.pod |
| 91 |
|
| 92 |
Enter number of choice: |
| 93 |
|
| 94 |
The command above shows a search for the keyword C<frobnicate>. Yes, |
| 95 |
that's a word used in the Perl documentation! It shows three hits and |
| 96 |
asks the user to enter a number between 1 and 3 to open the selected |
| 97 |
documentation page in a pager program (typically C<less>). In there, |
| 98 |
an in-text search for the expression can be started by using the C</> |
| 99 |
(slash) command. |
| 100 |
|
| 101 |
If two or more keywords are given, the search will yield pages that |
| 102 |
contain all of them. When searching for phrases, please include quotes |
| 103 |
(make sure to quote the quotes so the shell doesn't eat them): |
| 104 |
|
| 105 |
$ perldig '"floating point"' |
| 106 |
|
| 107 |
The underlying I<swish-e> search engine also understands expressions |
| 108 |
connected via AND and OR: |
| 109 |
|
| 110 |
$ perldig "'floating point' AND approximate AND 'real number'" |
| 111 |
|
| 112 |
To keep the index up to date, it is probably a good idea to run a |
| 113 |
cronjob every morning: |
| 114 |
|
| 115 |
00 4 * * * /usr/bin/perldig -u >/dev/null 2>&1 |
| 116 |
|
| 117 |
If you can read German, please check out this article in the "Linux- |
| 118 |
Magazin", where this script was originally published: |
| 119 |
|
| 120 |
http://www.linux-magazin.de/Artikel/ausgabe/2003/10/perl/perl.html |
| 121 |
|
| 122 |
=head1 EXAMPLES |
| 123 |
|
| 124 |
# Update/create the index |
| 125 |
$ perldig -u |
| 126 |
|
| 127 |
$ perldig frobnicate |
| 128 |
|
| 129 |
1) pod/perlguts.pod 2) pod/perlxstut.pod 3) pod/perlnewmod.pod |
| 130 |
|
| 131 |
Enter number of choice: 1 |
| 132 |
[ ... perlguts man page shows ... ] |
| 133 |
|
| 134 |
=head1 FILES |
| 135 |
|
| 136 |
C<perldig> puts the I<swish-e> index files into the folder C<.perldig> |
| 137 |
in the user's home directory. |
| 138 |
|
| 139 |
=head1 LEGALESE |
| 140 |
|
| 141 |
Copyright 2003-2005 by Mike Schilli, all rights reserved. |
| 142 |
This program is free software, you can redistribute it and/or |
| 143 |
modify it under the same terms as Perl itself. |
| 144 |
|
| 145 |
=head1 AUTHOR |
| 146 |
|
| 147 |
2003, Mike Schilli <[email protected]> |