in reply to Re: Pod: Need to parse from =begin ... =end blocks
in thread Pod: Need to parse from =begin ... =end blocks

Thanks, Toby. What follows is my first, admittedly crude adaptation of your code to my needs. Suggestions welcome.
Source file: MyConfig.pm
package MyConfig; use strict; use warnings; =head1 NAME MyConfig - Basic configuration data =head1 DESCRIPTION Configuration module. =head1 METHODS =head2 new() Constructor. =cut sub new { my ($class, $args) = @_; return bless $args, $class; } =head1 API Some description of API. =begin apidoc %basic = ( alpha => 'beta', gamma => 'delta', ); =end apidoc More description of API. =begin apidoc %ordinary = ( epsilon => 'zeta', eta => 'theta', ); =end apidoc =head1 SYSTEMS Some description of systems. =begin sysdoc %bold = ( iota => 'kappa', lambda => 'mu', ); =end sysdoc =cut 1;
Program: parse_docs.pl
#!/usr/bin/env perl use strict; use warnings; use 5.010; use Carp; use Data::Dumper;$Data::Dumper::Indent=1; { package Local::Podparser; use parent qw(Pod::Simple::PullParser); our %begins = (); sub _get_titled_section { my $self = shift; my @r; $self->{_in_get_titled_section} = 1; wantarray ? (@r = $self->SUPER::_get_titled_section(@_)) : ($r[0] = $self->SUPER::_get_titled_section(@_)); delete $self->{_in_get_titled_section}; wantarray ? @r : $r[0]; } sub get_token { my $self = shift; my $str = ''; $self->{output_string} = \$str; my $tok = $self->SUPER::get_token; if ( !$self->{_in_get_titled_section} and defined $tok and $tok->[0] eq 'start' and $tok->[1] eq 'for' ) { my $target = $tok->[2]{"target"}; my $data; until ($tok->[0] eq 'end' and $tok->[1] eq 'for') { $data .= $tok->[1] if $tok->[0] eq 'text'; $tok = $self->SUPER::get_token; } push @{$begins{$target}}, $data; $tok = $self->SUPER::get_token; } return $tok; } } my $source = q{./MyConfig.pm}; croak "Could not locate '$source'" unless (-f $source); my $parser = Local::Podparser->new; $parser->set_source( $source ); $parser->accept_targets('apidoc', 'sysdoc'); while (my $token = $parser->get_token()) { # cycle thru the tokens } say STDERR Dumper [ %Local::Podparser::begins ]; say "Finished";
Output: perl parse_docs.pl
$VAR1 = [ 'sysdoc', [ ' %bold = ( iota => \'kappa\', lambda => \'mu\', ); ' ], 'apidoc', [ ' %basic = ( alpha => \'beta\', gamma => \'delta\', ); ', ' %ordinary = ( epsilon => \'zeta\', eta => \'theta\', ); ' ] ]; Finished
Jim Keenan

Replies are listed 'Best First'.
Re^3: Pod: Need to parse from =begin ... =end blocks
by tobyink (Canon) on May 31, 2013 at 21:12 UTC

    Rather than our %begins, I'd suggest using %{$self->{_begins}}. Using a global hash would probably start to become annoying if you need to parse more than one pod document in a single run of the program.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name