Extract the Synopsis of a Module

May 10, 2011 / By Yanick Champoux

Tags: , ,

When I begin to work with a module, most of the time what I do is to look at its pod, and copy the code in the synopsis that I’ll use as a a baseline.

Open pod, copy, paste. That’s a lot of exhausting work… While I’m pretty sure there’s already a better tool to do it somewhere in CPAN, here’s my little podsyn script that does all the hard work for me.

#!/usr/bin/env perl

use strict;
use warnings;

use Pod::Find qw/ pod_where /;
use Pod::XML;
use XML::LibXML;

my $module = shift or die "usage: $0 <module>\n";

my $xml = do {
    local *STDOUT;
    open STDOUT, '>', \my $stdout;
    Pod::XML->new->parse_from_file( pod_where( { -inc => 1 }, $module ) );
    $stdout;
};

$xml =~ s/xmlns=".*?"//;

print XML::LibXML->load_xml( string => $xml )
        ->findvalue('//sect1[title/text()="SYNOPSIS"]/verbatim');

Its use on the command line is straigt-forward:

$ ./files/podsyn.pl Pod::XML

use Pod::XML;
my $parser = Pod::XML->new();
$parser->parse_from_file("foo.pod");

And crafting a vim command to do the same shouldn’t be too hard either (I’ll try to post one as soon as my vim-fu comes back to me).

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>