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.

True laziness is hard work

In reply to Re: Parsing text sections by GrandFather
in thread Parsing text sections by betacentauri

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.