Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

[perldoc] keyword search

by LanX (Saint)
on Aug 07, 2009 at 12:03 UTC ( [id://786762]=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

Problem:

perldoc has an option -f to search for functions but no possibility to search for other reserved words like operators (eg eq)... there isn't a -k switch for keywords like in man. And perldoc -f qr only produces a reference
See "Regexp Quote-Like Operators" in perlop.

Question:

I'm seeking now for hours for the best possibility to automatically jump into the relevant perldoc for a keyword! Before I start to reinvent the wheel I'd like to ask the holy monastery for wisdom...

Possible Approaches:

  1. Grepping in perltoc produces references to other perldocs
  2. perldoc mentions the possibility of a pod.idx file, which can be queried with the -X option. (... ah szabgab was also already wondering about pod.idx) ...
  3. some uncompleted ideas to use cached googlesearches

Cheers Rolf

Replies are listed 'Best First'.
Re: [perldoc] keyword search
by FunkyMonk (Chancellor) on Aug 07, 2009 at 14:21 UTC
      Hi

      Thanks, it's on CPAN as perldoc::search.

      The article link is broken, those interested might read it directly from Michael Schilli's - the author of Log4perl - website: http://perlmeister.com/snapshots/200310/index.html

      Anyway it's fulltext search, I tried to install the debian package but after a full-page warning about dependencies to install (including Log4perl ?!?) I gave up...shouldn't be such an alternative to googling.

      I was more interested in a keyword search and actually I found out that actual perldocs are already full of X<tags> to facilitate this search, but aren't evaluated yet. 8(

      just look at this snippet from perlop:

      =head2= Quote and Quote-like Operators X<operator, quote> X<operator, quote-like> X<q> X<qq> X<qx> X<qw> X<m> X<qr> X<s> X<tr> X<'> X<''> X<"> X<""> X<//> X<`> X<``> X<<< << >>> X<escape sequence> X<escape><P> While we usually think of quotes as literal values, in Perl they...

      Cheers Rolf

Re: [perldoc] keyword search
by thunders (Priest) on Aug 07, 2009 at 14:47 UTC
    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.

      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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://786762]
Approved by ww
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found