What I do in these situations is organise the rules into an array of hashes in the order for evaluation, something like: (updated for clarity)
use AnyLanguage; my @lexicon = [ ( REGEXP => regex1, ACTION => sub { code1 } ), ... ( ACTION => sub { default code } ) ]; my $MyLanguage = AnyLanguage -> new( \@lexicon ); ... while ( my $input = ... ) { $MyLanguage -> dispatch{ $input }; }
whose class (for all languages/rule sets) would be something like:
package AnyLanguage; sub new { my $self = shift; my $self -> { LEXICON } = shift; return bless $self; } sub dispatch{ # match down array and react accordingly my $self = shift; $_ = shift; my $aref = $self -> { LEXICON }; for ( my $i = 0; $i < $#$aref; $i++ ) { my $regexp = $aref -> [$i] -> { REGEXP }; /$regexp/ and return &($aref -> [$i] -> { ACTION }}; } return &{$aref -> [$#$aref ] -> { ACTION }}; } 1;
Update: Although as MyLanguage expands in complexity and usage, I would at some point in practice go one step further and package up all the 'action' code for language MyLanguage into its own MyLanguage class and simplify the action code into calls to methods of such a class, so that AnyLanguage ceases to be used directly by the 'outer' code, but by the methods in MyLanguage. In that case, the initialisation of the LEXICON for Mylanguage be moved to the new method of the MyLanguage class, before being passed on to the new method of the AnyLanguage class.

more update: it is also possible to maintain a separate dispatcher and parser (more likely wrapper for e.g. Parse::RecDescent) in the same AnyLanguage module, using a flag to tell AnyLanguage whether MyLanguage is dispatchable or not, (amongst possibly other flags for such critical language characteristic variations) to avoid having to duplicate code for different statement formats.

-M

Free your mind


In reply to Re: Mapping URLs to code - in search of perlish dispatch schemes by Moron
in thread Mapping URLs to code - in search of perlish dispatch schemes by Corion

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.