I don't see how a module could reduce the code substantially since only the dispatch loop is reusable.
You can sweep a lot of the nitty-gritty details under the carpet by using a module I wrote :)
use Regexp::Assemble; my $ra = Regexp::Assemble->new->track(1); my %dispatch = ( '^(?!START:|END:)([^*: ][^:]+):(.+)\n' => sub { my $r = shift; process_record($r); # first time will be empty record... %$r = (); # clear the record $r->{name} = $ra->mvar(1); $r->{desc} = $ra->mvar(2); }, '^START:\s*(.+)\n' => sub { $_[0]->{starttime} = $ra->mvar(1) }, '^END:\s*(.+)\n' => sub { $_[0]->{endtime} = $ra->mvar(1) }, '^(?:\* | )([^:]+):(\d+):(.+)\n' => sub { my @captures = @{$ra->mvar()}; push @{$_[0]->{items}}, [@captures[1..$#captures]], }, ); # add all the regular expressions to the R::A object $ra->add( keys %dispatch ); my $r = {}; # 'record' LINE: while (my $line = <DATA>) { # if we have a match, take the initial regular expression that match +ed # and use it as a key into our dispatch table and call its value. $ra->match($line) and &$dispatch{$ra->matched}->($r); } process_record($r);
I would be inclined to chomp $line, and remove the \n's from your expressions, but that's just personal taste.
The trick is that when you enable tracking on a Regexp::Assemble object, when a match is made, it is able to recover the initial pattern that triggered the match. Therefore, you can use it as a key into a dispatch table, and things will Just Work.
You have to use the mvar method to recover the values of $1, $2, $3... etc, because they are lost in scope by the time control flow gets back to your code. The first element returned by mvar() without a specfific parameter is another way of recovering the original pattern; it's a byproduct of walking down the @- and @+ arrays at the time of the match.
*ponders*
which leads me to conclude that there should be a method call that returns only the $1..$n array of captures...
- another intruder with the mooring in the heart of the Perl
In reply to Re: Munging with a regex dispatch table (with Regexp::Assemble)
by grinder
in thread Munging with a regex dispatch table
by Solo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |