Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: [perldoc] keyword search

by thunders (Priest)
on Aug 07, 2009 at 14:47 UTC ( #786806=note: print w/ replies, xml ) Need Help??


in reply to [perldoc] keyword search

I've also longed for a feature like this. I just cobbled this together, maybe this would be useful for you:

use strict; use IO::File; my $PODDIR = '/usr/share/perl/5.8.8/pod/'; my $CACHEDIR = "$ENV{HOME}/.podcache"; opendir(my $dir,$PODDIR) or die("failed to open pod directory"); unless(-d $CACHEDIR){ mkdir($CACHEDIR) or die("failed to build cache directory"); } while(my $f=readdir($dir)){ next unless $f =~ /\.pod$/; my $cache_file = "$CACHEDIR/$f.txt"; next if -f $cache_file; my $cache_fh = IO::File->new($cache_file,"w") or die("failed to buil +d cachefile $cache_file"); my @pod = `perldoc -t $PODDIR/$f`; for my $line (@pod){ print $cache_fh $line; } } my $pattern = $ARGV[0]; print `grep -r $pattern $CACHEDIR`

Set $PODDIR and $CACHEDIR to whatever values make sense on your system. And Here's how to use it. It accepts patterns grep accepts

username@servername$ perl podsearch qr/.*/ /home/username/.podcache/perl561delta.pod.txt: Dumping "qr//" o +bjects works correctly. /home/username/.podcache/perl5005delta.pod.txt: "lock" keyword", an +d "New "qr//" operator". /home/username/.podcache/perl5005delta.pod.txt: See "New "qr//" + operator". /home/username/.podcache/perl5005delta.pod.txt: New "qr//" operator /home/username/.podcache/perl5005delta.pod.txt: The "qr//" operator +, which is syntactically similar to the other /home/username/.podcache/perl56delta.pod.txt: Dumping "qr//" ob +jects works correctly. /home/username/.podcache/perl571delta.pod.txt: use MyFilter + qr/red/ => 'green'; /home/username/.podcache/perl571delta.pod.txt: +case qr/\w+/ { print "pattern" } /home/username/.podcache/perl58delta.pod.txt: use MyFilter +qr/red/ => 'green'; /home/username/.podcache/perl58delta.pod.txt: c +ase qr/\w+/ { print "pattern" } /home/username/.podcache/perlfaq6.pod.txt: separa +tor => qr/\s*,\s*/, /home/username/.podcache/perlfaq6.pod.txt: The qr// operator showed + up in perl 5.005. It compiles a regular /home/username/.podcache/perlfaq6.pod.txt: @patterns = map { qr +/\b$_\b/i } qw( foo bar baz ); /home/username/.podcache/perlfaq7.pod.txt: sufficiently recent +as to support the "qr//" construct, pass around /home/username/.podcache/perlfaq7.pod.txt: using "qr//": /home/username/.podcache/perlfaq7.pod.txt: $match = compare +("old McDonald", qr/d.*D/i); /home/username/.podcache/perlfaq7.pod.txt: Notice how "qr//" al +lows flags at the end. That pattern was compiled /home/username/.podcache/perlfaq7.pod.txt: at compile time, alt +hough it was executed later. The nifty "qr//" /home/username/.podcache/perlfaq7.pod.txt: here it is again if +you don't have "qr//": /home/username/.podcache/perlfunc.pod.txt: "m//", "pos", "quote +meta", "s///", "split", "study", "qr//" /home/username/.podcache/perlfunc.pod.txt: qr/STRING/ /home/username/.podcache/perlglossary.pod.txt: "q//", "qq//", " +qx//", "qw//", "qr//", "m//", "s///", "y///", and /home/username/.podcache/perlguts.pod.txt: r PERL_MAGIC_qr + vtbl_qr precompiled qr// regex /home/username/.podcache/perlop.pod.txt: change them, Perl +won't even notice. See also "qr/STRING/imosx". /home/username/.podcache/perlop.pod.txt: qr/STRING/imosx /home/username/.podcache/perlop.pod.txt: $rex = qr/my.S +TRING/is; /home/username/.podcache/perlop.pod.txt: $re = qr/$patt +ern/; /home/username/.podcache/perlop.pod.txt: my @compil +ed = map qr/$_/i, @$patterns; /home/username/.podcache/perlre.pod.txt: "s///", "qr//" and "??" in + "Regexp Quote-Like Operators" in perlop. /home/username/.podcache/perlre.pod.txt: used (see re), o +r the variables contain results of "qr//" /home/username/.podcache/perlre.pod.txt: operator (see "q +r/STRING/imosx" in perlop). /home/username/.podcache/perlre.pod.txt: 'Y|' => +qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ ); /home/username/.podcache/perlreref.pod.txt: qr/pattern/imsox lets + you store a regex in a variable, /home/username/.podcache/perlretut.pod.txt: part of the "m//", "s// +/", "qr//" and "split" operators and so this /home/username/.podcache/perlretut.pod.txt: can be stored once and +used again and again. The regexp quote "qr//" /home/username/.podcache/perlretut.pod.txt: does exactly that: "qr/ +string/" compiles the "string" as a regexp and /home/username/.podcache/perlretut.pod.txt: $reg = qr/foo+bar?/ +; # reg contains a compiled regexp /home/username/.podcache/perlretut.pod.txt: @compiled = map qr/ +$_/, @regexp; /home/username/.podcache/perlretut.pod.txt: $pat = qr/(?{ $foo += 1 })/; # precompile code regexp /home/username/.podcache/perltoc.pod.txt: 'STRING', qq/STRI +NG/ , "STRING", qr/STRING/imosx , qx/STRING/ , /home/username/.podcache/perltoc.pod.txt: ARRAY,LIST , , q/ +STRING/, qq/STRING/, qr/STRING/, qx/STRING/, /home/username/.podcache/perltoc.pod.txt: New "qr//" operator /home/username/.podcache/perltoc.pod.txt: As a simple string, A +s a qr// compiled regular expression, e.g.:, As /home/username/.podcache/perltoc.pod.txt: As a simple string, A +s a qr// compiled regular expression, e.g.:, As

Shelling out with a tainted variable like $ARGV[0] is not particularly safe. So you'll probably want to play with sanitizing that variable.


Comment on Re: [perldoc] keyword search
Select or Download Code
Replies are listed 'Best First'.
Re^2: [perldoc] keyword search
by LanX (Canon) on Aug 08, 2009 at 01:17 UTC
    Thanx, but grepping fulltext thru all pods wasn't really my intention...

    Anyway in bash it could be done as a one-liner

    # ALL OCCURENCES > grep -l 'qr//' /usr/share/perl/5.10/pod/*.pod /usr/share/perl/5.10/pod/perl5005delta.pod /usr/share/perl/5.10/pod/perl561delta.pod /usr/share/perl/5.10/pod/perl56delta.pod /usr/share/perl/5.10/pod/perlfaq6.pod /usr/share/perl/5.10/pod/perlfaq7.pod /usr/share/perl/5.10/pod/perlfunc.pod /usr/share/perl/5.10/pod/perlglossary.pod /usr/share/perl/5.10/pod/perlguts.pod /usr/share/perl/5.10/pod/perlop.pod /usr/share/perl/5.10/pod/perlreapi.pod /usr/share/perl/5.10/pod/perlreguts.pod /usr/share/perl/5.10/pod/perlre.pod /usr/share/perl/5.10/pod/perlretut.pod /usr/share/perl/5.10/pod/perltoc.pod

    # JUST THE X TAGS WITH qr > grep -l 'X<qr>' /usr/share/perl/5.10/pod/*.pod /usr/share/perl/5.10/pod/perlop.pod /usr/share/perl/5.10/pod/perltoc.pod

    # AUTOMATICALLY OPENING THE SEARCHRESULTS IN PERLDOC > for i in `grep -l 'X<qr>' /usr/share/perl/5.10/pod/*.pod`;do perldoc + $i;done

    You can even tell the pager to search within the file for the pattern... 8)

    Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://786806]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (17)
As of 2015-07-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls