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; #### 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