Reputation: 40758
I am trying to extract the Pod documentation from a Perl file. I do not want to convert the documentation to text as is done by Pod::Simple::Text
. I just want the Pod text as Pod text, such that I can feed it into Pod::Template
later. For example:
use warnings;
use strict;
use Pod::Simple::Text;
my $ps=Pod::Simple::Text->new();
my $str;
$ps->output_string( \$str );
$ps->parse_file($0);
print $str;
__END__
=head1 SYNOPSIS
prog [OPTIONS]
This will print the Pod as text. Is there a CPAN module that can give me the Pod text, that is:
=head1 SYNOPSIS
prog [OPTIONS]
instead?
Update
The solution should be able to handle Pod docs in strings, like
my $str = '__END__
=head1 SYNOPSIS';
Upvotes: 1
Views: 602
Reputation: 9
Why not simply grepping the pods from the file:
open SR, $0;
print grep /^=/../^=cut$/, <SR>;
close SR;
Upvotes: 0
Reputation: 35198
This can be done using PPI
:
use strict;
use warnings;
use PPI;
# Slurp source code
my $src = do { local ( @ARGV, $/ ) = $0; <> };
# Load a document
my $doc = PPI::Document->new( \$src );
# Find all the pod within the doc
my $pod = $doc->find('PPI::Token::Pod');
for (@$pod) {
print $_->content, "\n";
}
=comment
Hi Pod
=cut
1;
__END__
=head1 SYNOPSIS
prog [OPTIONS]
Outputs:
=comment
Hi Pod
=cut
=head1 SYNOPSIS
prog [OPTIONS]
Upvotes: 3
Reputation: 9269
Use the -u
option for perldoc
. This strips out the POD and displays it raw.
If you want to extract the POD from within a Perl program, you could do something like this:
my $rawpod;
if (open my $fh, '-|', 'perldoc', '-u', $filename) {
local $/;
my $output = <$fh>;
if (close $fh) {
$rawpod = $output;
}
}
If you really don't want to run perldoc
as an executable, you might be interested that the perldoc
executable is a very simple wrapper around Pod::Perldoc
which you might want to consider using yourself.
Upvotes: 4
Reputation:
Pod::Simple::SimpleTree will give it to you as a parse tree. You can convert that back to POD source easily enough.
Upvotes: 1