重构POD文档

时间:2014-10-29 07:44:00

标签: perl

我有一些几乎相同的Perl脚本,因此使用文档几乎相似。例如,prog1.pl可以是:

use warnings;
use strict;
use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);

my $help;

GetOptions(help=>\$help);
pod2usage( -message => "", -verbose => 2, -output => \*STDERR ) if $help;

__END__

=head1 SYNOPSIS

prog [OPTIONS]

=head1 OPTIONS

=over 4

=item --help

Print this summary.

=back

=head1 DESCRIPTION

A program

现在prog2.pl是一个类似的脚本(此处未显示)。让我们说它有一组选项的相同文档。对于这个虚拟示例,只要说两个脚本的help选项的文档是相同的。如何重构该文档字符串(即字符串"Print this summary."),以便我不必在prog1.plprog2.pl中重复相同的字符串?

2 个答案:

答案 0 :(得分:1)

This link指的是两个可以帮助您使用POD文档模板的模块:Pod::TemplatePod::Weaver

自动从模板生成POD文档,并使用pod2usage选项将生成的POD文件的文件名提供给-input

答案 1 :(得分:0)

以下是使用Pod::Template的可能解决方案。首先使用:

创建文件help.pod
=Template help

=item B<--help>

Print this summary.

=back

然后我们使用以下方法将其包含在程序的使用POD文档中:

use warnings;
use strict;
use Pod::Template;
use File::Spec::Functions qw(catfile);
use Getopt::Long qw(GetOptions);
BEGIN { $Pod::Usage::Formatter = 'Pod::Text::Termcap'; }
use Pod::Usage qw(pod2usage);
use IO::String;

my $parser = Pod::Template->new();
$parser->parse( template => $0 );

my $io = IO::String->new($parser->as_string);
my $help;

GetOptions(help=>\$help);
pod2usage( -message => "", -verbose => 2, -output => \*STDERR, -input => $io) if $help;


__END__

=Include help.pod as Help

=head1 SYNOPSIS

<prog> [OPTIONS]

=head1 OPTIONS

=over 4

=Insert Help->help

=head1 DESCRIPTION

A program