package Parser::ReportFoo; use strict; sub new { ... $self->columns = [qw(date customer amount)]; }; my @handlers = [ # Line containing uninteresting information [qr/^Line containing uninteresting information/ => \&discard], [qr/^------------/ => \&discard], [qr/^(\d\d-[A-Z]{3}-\d\d) (..........) (\d+\.\d+)/ => \&capture_transaction], [qr/^\s*$/ => \&flush ], [qr/^ ) (..........)\s+$/ => \&capture_name2], ]; sub discard {}; sub flush { my ($self) = @_; my @row = map { $self->$_ } (@{ $self->columns }); print join "\t", @row; }; sub capture_transaction { my ($self,$date,$customer,$amount) = @_; $self->date($date); $self->customer($customer); $self->amount($amount); }; sub capture_name2 { my ($self,$customer) = @_; $self->customer($self->customer . " " . $customer); $self->flush(); }; sub parse { my ($self,$file) = @_; my $fh = open "<", $file or die "$file: $!"; while (defined my $line = <$fh>) { # First, check which regex my $handled; for (@handlers) { my ($re,$code) = @$_; if (my @match = ($line =~ /$re/)) { $code->($self, @match); $handled = 1; last; }; }; warn "Unhandled line >>$line<<"; }; };