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

Is there a way to display code without the Perldoc? Sort of a reverse perldoc or perldoc --code or perldoc --code_only type situation. Sometimes all the perldocs get in the way of reading the code, and it would be nice to have the version of code without them.


I admit it, I am Paco.

Replies are listed 'Best First'.
Re: perldoc code only
by busunsl (Vicar) on Jan 07, 2002 at 17:39 UTC
    AFAIK there is no switch to get what you want.

    Try the following:

    perldoc -m MyModule | perl -n -e " next if (/^=/ .. /^=cut/); print"
    It does roughly what you want. It is a quick oneliner, so no error checking and such.
Re: perldoc code only
by MZSanford (Curate) on Jan 07, 2002 at 18:55 UTC
    Another possible way to do it would be to use Deparse ... i tested this with the following code called test.pl :
    #!/usr/bin/perl use strict; my $a = &foo(); print $a; =head here is some pod =cut sub foo { return('foo'); }

    Then, to verify Deparse does what i expected, i did the following :
    prompt > perl -MO=Deparse test.pl my $a = &foo(); print $a; sub foo { return 'foo'; } test.pl syntax OK prompt >
    There is a "down" side to this. This does not give the code as it was typed, but as Perl will see it. See the Deparse page for more info. If this is not acceptable (e.g. you want the original code), use something like the post from busunsl

    update :Fixed Opera for Unix post problem
Re: perldoc code only
by jmcnamara (Monsignor) on Jan 07, 2002 at 20:29 UTC

    perldoc is designed to search for files that contain Pod. If the code isn't in the same file as the Pod then perldoc won't give you what you want. For example:      perldoc -m Inline For most cases the following will probably do what you want:     perl -ne '/^=/../^=cut/ or print' file

    --
    John.

Re: perldoc code only
by hotshot (Prior) on Jan 07, 2002 at 18:43 UTC
    In updating what I already suggested, you don't have to define a note, just don't print anything between =pod and =cut.

    Hotshot
Re: perldoc code only
by hotshot (Prior) on Jan 07, 2002 at 17:15 UTC
    I'm not sure I understood your question exactly, but I'll try anyway. You can put a note (a constant or something) In your code between the code and the perldoc, and write a little script that gets a script filename as parameter and prints It's contents until it reaches the note (if the perldoc is at the end of your script, otherwise you print after you found the note).

    Hotshot
(crazyinsomniac) Re: perldoc code only
by crazyinsomniac (Prior) on Jan 09, 2002 at 11:15 UTC
Re: perldoc code only
by seattlejohn (Deacon) on Jan 07, 2002 at 23:38 UTC
    I suspect I know just what you are looking for; after I posted a module last night, mkmcconn mentioned that he found the inline POD very hard to read, so I whipped up a quick-and-dirty little script that takes POD embedded in a file and moves it all down to the end. Not tested too extensively, but it basically seems to work:
    die "Usage: SegregatePod filename\n" unless @ARGV == 1; open INPUT, $ARGV[0] or die "Unable to open $ARGV[0]: $!\n"; my @paragraphs = do {local $/ = ""; <INPUT>}; close INPUT; my $inside_pod = 0; my $extracted_pod = ''; my $leftover_code = ''; foreach my $this_paragraph (@paragraphs) { if (substr($this_paragraph,0,4) eq '=cut') { $inside_pod = 0; $this_paragraph = ''; # =cut no longer needed since POD sections w +ill be contiguous } elsif (substr($this_paragraph,0,1) eq '=') { $inside_pod = 1; } if ($inside_pod) { $extracted_pod .= $this_paragraph; } else { $leftover_code .= $this_paragraph; } } print "$leftover_code\n$extracted_pod\n";

    Of course you could simply omit $extracted_pod from the last line if you didn't want to see it at all.

    As for the inline code, I've learned my lesson, and future submissions will have the POD at the end ;-)