Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

raw_data with Pod::Simple::PullParser

0 views
Skip to first unread message

RAPPAZ Francois via pod-people

unread,
Mar 28, 2017, 5:45:07 AM3/28/17
to pod-p...@perl.org

I have a small file of pod documentation and I would like to have a hash with keys being the head1 text (NAME, VERSION, AUTHOR etc) and value being the raw data of the lines read up to the next =head1.

 

I have subclassed Pod::Simple::PullParser to have the text value after head1, but my problem is to have the unparsed lines (including this =head1 XXX)

 

I saw that there is a raw_mode in the the _accessorize list in Pod::Simple and I have tried this code in my subclass

 

package Myparser;

use base qw(Pod::Simple::PullParser);

sub new {

     my $self = shift;

  my $new = $self->SUPER::new(@_);

  $new->{'output_fh'} ||= *STDOUT{IO};

     $new->_accessorize('raw_mode'=>1);

     return $new;

 

}

 

But this does not seems to change the ouput of $token->text for example.

 

Thanks for any help

 

François

 

Ron Savage

unread,
Mar 28, 2017, 5:45:02 PM3/28/17
to pod-p...@perl.org
Hi François

Can't help specifically but I do suggest you use a tree rather than a
hash. It just makes much more sense to me to store the pod that way.

--
Ron Savage - savage.net.au

RAPPAZ Francois via pod-people

unread,
Mar 29, 2017, 2:15:02 AM3/29/17
to pod-p...@perl.org
Hi Ron,

Thanks for the suggestion.

At the end, reading the pod file and capturing the key from head1 is easy (below). I could use a Pod::Simple::SimpleTree , but I would still to have the output as raw pod. Probably using a parser to select pars of pod is not a good idea ?

François

my $fname = shift;
my $mod;
open $mod, "<$fname" or die "Can't open $fname $! \n";
my %data;
my $data_ar;
my $key;
my $ispod;
for my $line (<$mod>) {
chomp $line;
if ( $line =~ /^=cut/ ) { $ispod = 0; }
if ( $line =~ /^=head1\s*(.+)$/i ) {
$ispod = 1;
if ($key) { #$key read from the previous =head1
$data{$key} = $data_ar; #store the array of lines as an array ref
$data_ar = undef; # and prepare a new array
}
$key = $1; #now fetch the new value for $key
}
push @{$data_ar}, $line if ($ispod) ; #store the line in the array if we are in a pod section

}
close $mod;
$data{$key} = $data_ar; #fetch the lines from the last =head1 read
=for comment
for $key ( keys %data ) {
print "*$key*\n";
print join( "\n", @{ $data{$key} } ), "\n";

}
=cut
return %data;

Ron Savage

unread,
Mar 29, 2017, 2:30:02 AM3/29/17
to pod-p...@perl.org
Hi Francois

Do what works. If you're only parsing your own POD, then what you're
doing now should be OK.

I would like (one day!) to write a BNF for POD and use Marpa:

http://savage.net.au/Marpa.html

About 10 of the Perl packages there are mine.

If you intend to create a module fit for CPAN, then of course it's a
bigger decision as to the internal structure of your code.
0 new messages