Solo has asked for the wisdom of the Perl Monks concerning the following question:
command(s) | parse.pl | process.pl
where process.pl is one of a few possible processing scripts.
For parse.pl, I'm pondering how to best apply a dispatch table approach. Here is what I have now (simplified example).
use strict; use warnings; use YAML; my $header = qr/^(?!START:|END:)([^*: ][^:]+):(.+)\n/; my $starttime = qr/^START:\s*(.+)\n/; my $endtime = qr/^END:\s*(.+)\n/; my $lineitem = qr/^(?:\* | )([^:]+):(\d+):(.+)\n/; my %dispatch = ( $header => \&header, $starttime => sub { $_[0]->{starttime} = $_[1] }, $endtime => sub { $_[0]->{endtime} = $_[1] }, $lineitem => sub { push @{shift->{items}}, \@_ }, ); sub header { my $r = shift; process_record($r); # first time will be empty record... %$r = (); # clear the record $r->{name} = $_[0]; $r->{desc} = $_[1]; } my $r = {}; # 'record' LINE: while (my $line = <DATA>) { for my $re (keys %dispatch) { my @m = (); if (@m = $line =~ /$re/) { $dispatch{$re}->($r, @m); next LINE; } } } process_record($r); sub process_record { my $r = shift; print YAML::Dump($r); } __DATA__ The first record:it has only 1 item START: Tue Feb 1 00:09:30 2005 END: Tue Feb 1 00:19:32 2005 Item1:10:comment1 The next record:might have more items START: Tue Feb 1 00:39:07 2005 END: Tue Feb 1 00:42:46 2005 Item1:4:comment2 * Item2:1:comment3
I'm happy to have removed the if/elsif madness, but now the dispatch table definition looks almost as ugly. How would you 'clean' it up? I've considered pulling some bits into a module or building the dispatch table out of a config file. The config file worries me because it would require putting data and code in the config. I don't see how a module could reduce the code substantially since only the dispatch loop is reusable. Thoughts?
--Solo
--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Munging with a regex dispatch table
by Tanktalus (Canon) on Feb 01, 2005 at 21:10 UTC | |
by Fletch (Bishop) on Feb 01, 2005 at 21:17 UTC | |
|
Re: Munging with a regex dispatch table
by Fletch (Bishop) on Feb 01, 2005 at 21:04 UTC | |
|
Re: Munging with a regex dispatch table
by nmerriweather (Friar) on Feb 01, 2005 at 22:33 UTC | |
by Anonymous Monk on Feb 02, 2005 at 03:04 UTC | |
by Tanktalus (Canon) on Feb 02, 2005 at 05:15 UTC | |
|
Re: Munging with a regex dispatch table (with Regexp::Assemble)
by grinder (Bishop) on Feb 02, 2005 at 22:40 UTC | |
by Solo (Deacon) on Feb 03, 2005 at 15:49 UTC |