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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.