I have some 36 Perl modules to document, with one subroutine in them each. This particular project is for my use only, but it should be documented for my sanity (and so Devel::Cover likes me better).

I was planning on adding stub documentation automatically, but then I realized I could have it prompt me for the documentation. This code tries to see if a subroutine is already documented, but not with Pod::Coverage.

If nothing else like this exists and there is sufficient interest, I might expand it into a real module to help build documentation.

Warning: This code does not make backup copies.

#!/usr/bin/perl use strict; use warnings; use PPI; foreach my $filename (@ARGV) { print "Opening up $filename...\n"; my $doc = PPI::Document->new($filename); foreach my $sub (@{$doc->find('PPI::Statement::Sub')}) { my $subname = $sub->name; my $previous = $sub->previous_sibling; $previous = $previous->previous_sibling while $previous && !$p +revious->isa('PPI::Token::Pod'); if($previous && $previous->isa('PPI::Token::Pod') && $previous +->{'content'} =~ /=(?:head\d|item)\s+$subname/i) { print "skipping $subname; already documented\n"; next; } my $pod = PPI::Token::Pod->new; print "Enter documentation for $subname (ctrl+D when done, lea +ve blank to skip):\n"; my $text = join '', <STDIN>; $text =~ s/^\n+|\n+$//g; unless($text) {print "skipping $subname; no text provided"; ne +xt} $pod->{'content'} = "=head2 $subname()\n\n$text\n\n=cut\n\n"; $sub->insert_before($pod); } print "Writing code to $filename...\n"; $doc->save($filename); # print $doc->serialize; }

Replies are listed 'Best First'.
Re: Using PPI to document existing modules
by Your Mother (Archbishop) on Apr 12, 2008 at 08:13 UTC

    I did not play around with your code but I find the idea intriguing and I hope you pursue it. Guided stuff like this, where the human-only component (like documentation) is programmatically conducted, rules. Anything to help devs do more documentation with less hassle is a good idea.

Re: Using PPI to document existing modules
by hsmyers (Canon) on Apr 15, 2008 at 05:58 UTC
    Yes! Do continue to develop this. ANY contribution to documentation either for yourself or others is certainly a step in the right direction. While I see that your output is POD, you might want to think about other possibilities in addition. At the moment, I am re-writing one of my CPAN modules in Lisp. The dialect I'm using (newLISP) uses a comment embedded mini-language to produce a nicely formatted html page. You might try the same. If you do then I would point out that perltidy can output formatted and colored html text given functions as input; and of course this could be folded back into the documentation. All of this could be kept within the source file; something that could prevent the drift between source and documentation. Enough babble. Go for it. --hsm

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
      The main improvement that I want to develop is the use of PPI to automatically add POD inline with the Perl code. I'm only interested in Perl right now, since that's what I'm using. If there are other modules out there in Perl to parse Language X, then that could be worth adding. I'm not trying to replace Pod::HTML or create a non-POD format for Perl.

      I have some free time coming up in a month or so, and I'm interested in making this into something that can add all the sections that documentation needs (name, synopsis, description, methods, see also, bugs, author).

        I mentioned the Lisp part as an example of what you could do in Perl. Nothing was said about you targeting other languages with this or any other code. Sorry to have made the suggestion.

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."