If I understand correctly what you are trying to achieve then the following may help:
use strict; use warnings; package Parser; my %parsers; sub new { my ($class) = @_; return bless {}, $class; } sub parse { my ($self, $file) = @_; local $/ = "------------------------------------------------------ +---------------------\n\@---"; while (my $record = <$file>) { chomp $record; $record =~ s/-+$//; next if ! length $record; next if $record !~ s/^\s*(\w+)([^-]+)[-\n]+//; my ($type, $tail) = ($1, $2); die "Can't deal with $type record. Header is '$type$tail'\n" if ! exists $parsers{$type}; $parsers{$type}->($self, $record); } } sub registerParser { my ($type, $parser) = @_; $parsers{$type} = $parser; } package Callsites; use parent -norequire, 'ParserBase'; Parser::registerParser (__PACKAGE__, \&Callsites::parser); sub parser { my ($host, $record) = @_; print __PACKAGE__, " parser processing:\n$record\n"; } package Aggregate; Parser::registerParser (__PACKAGE__, \&Aggregate::parser); sub parser { my ($host, $record) = @_; print __PACKAGE__, " parser is processing:\n$record\n"; } package main; my $data = <<'D'; ---------------------------------------------------------------------- +----- @--- Callsites: 2 ---------------------------------------------------- +----- ---------------------------------------------------------------------- +----- ID Lev File/Address Line Parent_Funct MPI_Call 1 0 0x8048ad5 [unknown] Reduce 2 0 0x8048a3b [unknown] Bcast ---------------------------------------------------------------------- +----- @--- Aggregate Time (top twenty, descending, milliseconds) ----------- +----- ---------------------------------------------------------------------- +----- Call Site Time App% MPI% COV Bcast 2 9.5 24.71 65.75 0.59 Reduce 1 4.95 12.87 34.25 1.35 ---------------------------------------------------------------------- +----- D my $parser = Parser->new (); open my $inFile, '<', \$data; $parser->parse ($inFile); close $inFile;
Prints:
Callsites parser processing: ID Lev File/Address Line Parent_Funct MPI_Call 1 0 0x8048ad5 [unknown] Reduce 2 0 0x8048a3b [unknown] Bcast Aggregate parser is processing: Call Site Time App% MPI% COV Bcast 2 9.5 24.71 65.75 0.59 Reduce 1 4.95 12.87 34.25 1.35
Note the use of perlvar $/ to break the input into records. I've assumed that the record type can be inferred from the first word of the record header. If that's not the case you'll have to generate a unique parser identifier for each record type in some other fashion.
The important element however is for the parser class to use a lookup hash for the parser types and for each parser type to register itself. Of course the implementations for the different parser types could be in separate modules - I've lumped em all together in one file to make the sample code more cohesive.
Passing $host into the parser implementation subs allows parsed information to be passed back in some fashion. I'm a little unclear about what you want to do with the parsed data,but I'm sure you can figure that out.
In reply to Re: Parsing text sections
by GrandFather
in thread Parsing text sections
by betacentauri
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |