Here is an example reading from a manpage in DATA

$_ = join'', <DATA>; print m/^DESCRIPTION(.*?)\n\n/ms;; __DATA__ NAME perldoc - Look up Perl documentation in pod format. SYNOPSIS perldoc [-h] [-v] [-t] [-u] [-m] [-l] [-F] [-X] PageName|ModuleName|ProgramName perldoc -f BuiltinFunction perldoc -q FAQ Keyword DESCRIPTION *perldoc* looks up a piece of documentation in .pod format that is embedded in the perl installation tree or in a perl script, and displays it via `pod2man | nroff -man | $PAGER'. (In addition, if running under HP-UX, `col -x' will be used.) This is primarily used for the documentation for the perl library modules. Your system may also have man pages installed for those modules, in which case you can probably just use the man(1) command. OPTIONS -h help Prints out a brief help message.

We get away with the one liner because in list context a regexp returns a list of what it matched so effectively we are dining a m/(this)/; print $1. We need the /m and /s modifiers. For details on why see perlman:perlre To get the entire output of the man command into a var like $_ the easiset way is to use backtics like this $_ = `man $manpage`;. Don't let just anyone supply the input value to $manpage as nasty input can do nasty things in bactics. A value like '; rm -rf *;' might be suboptimal. A short program to do this might be:

#!/usr/bin/perl -w use strict; print "Which manpage? "; chomp ( my $manpage = <> ); # only allow say words and numbers in $manpage die "Illegal input" unless $manpage =~ m/^\w+$/; $_ = `man $manpage`; print m/^DESCRIPTION(.*?)\n\n/ms;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: grepping man pages by tachyon
in thread grepping man pages by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.