sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
bless ($self, $class);
return $self;
}
####
sub file {
my $self = shift;
if (@_) {
$self->{file} = shift;
}
}
####
sub _common {
my $self = shift;
(my $key = (caller(1))[3]) =~ s/.*:://;
if (@_) {
$self->{$key} = shift;
}
else {
$self->{$key}->($self) if defined $self->{$key};
}
}
####
sub _common {
my $self = shift;
my $key = shift;
# ...
####
sub pre {
my $self = shift;
$self->_common(pre => @_);
}
# etc.
####
$self->pre();
open my $fh, '<', $self->{file} or croak "could not open $self->{file}: $!";
while (my $record = <$fh>) {
chomp $record;
$self->{record} = $record;
$self->loop();
$self->{hash}{ $self->{key} } = $self->{value};
++$self->{count};
}
$self->post();
####
my %data = $self->loop();
@{$self->{hash}{keys %data}} = values %data;
$count += scalar keys %data;