<div dir="ltr"><div><div><div>My preferred approach is to use a class:<br></div><div>This untested example uses named captures which are a recent perl feature.<br></div><div>the Regexp /extract (?<method> .*+) from string/ is "meta syntax" for a regexp that finds a method name in the input string. <br></div><div><br></div><div style="margin-left:40px">package Switch;<br><br></div><div style="margin-left:40px">sub add {...}<br></div><div style="margin-left:40px">sub delete {...}<br></div><div style="margin-left:40px">sub frobnicate {...}<br></div><div style="margin-left:40px">sub AUTOLOAD { die "unimplemented method called" }<br></div><div style="margin-left:40px"><br></div><div style="margin-left:40px">package Main;<br></div><div style="margin-left:40px"><br></div><div style="margin-left:40px">while (my $line = <>) {<br>   $line =~ /extract (?<method> .*+) from string/;<br></div></div></div><div style="margin-left:40px">   my $method = $+{method} or next; $ ignore lines that have no method<br></div><div style="margin-left:40px">   Switch->$method($line);<br></div><div style="margin-left:40px"><div><div><div>}<br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 19, 2016 at 2:28 PM, Robert L. Harris <span dir="ltr"><<a href="mailto:robert.l.harris@gmail.com" target="_blank">robert.l.harris@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div>Anyone have a straight forward script using dispatch lists?  I have one ( 4500 lines by now ) which effectively does this:</div><div><br></div><div><ol><li>Open input file</li><li>while<input> {</li><li>    $Line=<INPUT></li><li>    &Sub1("Line") if ( $Line =~ /<regex pattern 1>/ );</li><li>    &Sub2("Line") if ( $Line =~ /<regex pattern 2>/ );</li><li>    &Sub3("Line") if ( $Line =~ /<regex pattern 3>/ );</li><li>    ... about 25 patterns now ...</li><li>}</li></ol><div>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.  </div></div><div><br></div><div>   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.</div><div><br></div><div>Any examples would be very welcome.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Robert</div></font></span></div>
<br>_______________________________________________<br>
Denver-pm mailing list<br>
<a href="mailto:Denver-pm@pm.org">Denver-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/denver-pm" rel="noreferrer" target="_blank">http://mail.pm.org/mailman/listinfo/denver-pm</a><br>
<br></blockquote></div><br></div>