[Denver-pm] Dispatch Lists?

Chris Fedde chris at fedde.us
Mon Apr 25 17:51:03 PDT 2016


My preferred approach is to use a class:
This untested example uses named captures which are a recent perl feature.
the Regexp /extract (?<method> .*+) from string/ is "meta syntax" for a
regexp that finds a method name in the input string.

package Switch;

sub add {...}
sub delete {...}
sub frobnicate {...}
sub AUTOLOAD { die "unimplemented method called" }

package Main;

while (my $line = <>) {
   $line =~ /extract (?<method> .*+) from string/;
   my $method = $+{method} or next; $ ignore lines that have no method
   Switch->$method($line);
}

On Tue, Apr 19, 2016 at 2:28 PM, Robert L. Harris <robert.l.harris at gmail.com
> wrote:

>
> Anyone have a straight forward script using dispatch lists?  I have one (
> 4500 lines by now ) which effectively does this:
>
>
>    1. Open input file
>    2. while<input> {
>    3.     $Line=<INPUT>
>    4.     &Sub1("Line") if ( $Line =~ /<regex pattern 1>/ );
>    5.     &Sub2("Line") if ( $Line =~ /<regex pattern 2>/ );
>    6.     &Sub3("Line") if ( $Line =~ /<regex pattern 3>/ );
>    7.     ... about 25 patterns now ...
>    8. }
>
> Yeah, it's ugly, it started out as a 30 line data munger about 2.5 years
> ago and I'm looking to speed and clean it up.   Each Sub performs various
> actions based which can't be simplified or condensed more than they have.
>
>    I've created my DispatchHash for subs/patterns but I think I'm
> confusing myself on actually doing the match and dispatch including passing
> $Line to the sub.
>
> Any examples would be very welcome.
>
> Robert
>
> _______________________________________________
> Denver-pm mailing list
> Denver-pm at pm.org
> http://mail.pm.org/mailman/listinfo/denver-pm
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/denver-pm/attachments/20160425/5bc20b21/attachment.html>


More information about the Denver-pm mailing list