[Oc-pm] Chain of subs...
Wilson, Douglas
dgwilson at sonomasystems.net
Fri Jul 9 13:16:48 CDT 2004
> > > I'm writing a log parser for some things at work, and am
> > > trying to figure
> > > out the best way to run each line of the log through a chain
> > > of subroutines.
> > >
> > > Is an array of sub references the best way to go for this?
> >
> > That seems as good a way to go as any. Then you can easily
> > add or remove any of the parsing actions. But if you
> > are doing some of the same things in each subroutine (e.g.
> > parsing the line into tokens), then you might think about
> > doing some of the work before passing off the rest of the
> > work to the subs.
>
> Oops, I meant to put that into my initial e-mail.
>
> I'm parsing out the lines into a hash table first, and then
> I'll pass a
> reference to the hash table to each of the functions.
I'm all for keeping it simple, but if you wanted to, you
could go OO and write a dispatching class something like:
package MyLogDispatcher;
sub new {
my $class = shift;
bless [@_], $class
}
sub process {
my $self = shift;
# tokenize @_ to %args or whatever
$_->process(%args) for @$self;
}
package MyLogProcessor1;
sub process {
my $self = shift;
# process (@_)
}
package MyLogProcessor2;
sub process {
my $self = shift;
# process (@_)
}
package main;
my $processor = MyLogDispatcher->new(qw(
MyLogProcessor1
MyLogProcessor2
));
while (<>) {
$processor->process($_);
}
More information about the Oc-pm
mailing list