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

So.. I was trying out Pod::POM ...
#!/usr/bin/perl use base 'LEOCHARRE::CLI'; use strict; use Pod::POM; use Cwd; my $o = gopts(); #my $files = argv_aspaths(); my @sections = qw(name synopsis description subroutines methods author + bugs license); #for my $abs (@$files){ my $abs = Cwd::abs_path($ARGV[0]); my $parser = new Pod::POM; print STDERR "File $abs.\n"; my $pom = $parser->parse($abs) or die; my %section; for my $h ( $pom->head1 ) { my $name = $h->title; my $cont = $h->content; $section{ $name } = $cont; debug("found section $name"); } for (@sections){ my $section = uc($_); my $have = exists $section{$section} ? 1 : 0; print STDERR "Section '$section' is present? $have\n"; unless($have){ #push @{$pom}, # ?????? } } print STDERR "\n"; # view print $pom->present;

It takes a path to a file with pod, shows some little things.. And prints the output as parsed by Pod::POM, (could be slightly modified).

I was wondering.. How would I got about inserting a pod section?

My goal is to have some scripting that checks my modules and find out what parts are missing, etc, and inserts them for me. I have some code that will analize a module and create the pod, but in this case, I have pre-existing pod that I would like to check, maybe alter and spit to stdout..

Is there some other Pod:: module I should be taking a better look at? Tons on cpan, dizzying..

Replies are listed 'Best First'.
Re: check pod and insert missing sections
by pc88mxer (Vicar) on May 01, 2008 at 22:16 UTC
    I haven't completely figured it out, but I have determined that you can add a head1 with code like this:
    use Pod::POM; my $parser = Pod::POM->new(); my $pom = $parser->parse_file(...); my $h1 = $pom->add($parser, 'head1', 'This is a new head1');
    and the new head1 will appear at the end of the POD. However, I don't think it will be easy to add a a head1 section in the middle of the document (in case you want your sections to follow a uniform ordering). In that case, something like this might work for you:
    my @SECTIONS = qw(name synopsis ...); my $pom = $parser->parse_file(...); my %found; for my $h1 ($pom->head1()) { $found{lc($h1->title)} = $h1; } for my $title (@SECTIONS) { my $h1; if ($h1 = $found{$title}) { print $h1; } else { print "=head1 ", uc($title), "\n\n"; } }
    Of course, this code silently discards sections that are not listed in @SECTIONS which is probably not what you want to do, but it at least is a start.

      If I want to insert in middle, I could have an 'inject()' method, or override 'add()' to accept a possible index attrib.. or.. who knows.

      And I don't mind rebuilding the entire doctree every time there is an edit either.

      Seems this could be of use to others? If I find out anything good or make something worthy, I'll update.

      Thank you!