<div dir="ltr"><div>I also don't see how dispatch tables violate DRY. I think the better way to consider them as a special case statement (without fallthrough).</div><div><br></div><div>Consider perlbrew. Some of the commands are init, install, switch and use.<br></div><div><br></div><div>if ( $ARGV[0] eq 'init' ) { initialize() }</div><div>else if ( $ARGV[0] eq 'install' ) { install(@ARGV) }</div><div>...</div><div>else { say 'Not a valid command' }</div><div><br></div><div>or<br><br></div><div>use Switch;</div><div>switch( $ARGV[0] ){</div>
<div>    case 'init' { initialize() }<br></div>


<div>    case 'install' { install( @ARGV ) }</div><div>...<br></div>
<div>    else { say "Not a valid command" }<br></div>



}<br><div><br></div><div>becomes <br><br></div><div>my %dispatch {</div><div>    'init' => &initialize ,</div><div>    'install' => &install,<br></div><div>}</div><div><br></div><div>if ( defined $dispatch{ $ARGV[0] } ) {</div><div>    &$dispatch(@ARGV) ;<br></div><div>} else { say "Not a valid command" }</div><div><br></div><div>With some playing around, you can check $ARGV[0] against keys %dispatch, finding the Levenshtein distance, to respond to a typo like "imit" with 'did you mean "init"?' This last way *IS* pretty much how perlbrew works.<br><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, May 28, 2019 at 3:25 PM Joe Kline <<a href="mailto:gizmo@purdue.edu">gizmo@purdue.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Mark,<br>
<br>
I'm not sure how a typical dispatch table is violating DRY.<br>
<br>
What I typically do is something like:<br>
<br>
my ($dispatch_table) = create_dispatch_table($config_info_if_needed);<br>
<br>
sub create_dispatch_table ($config) {<br>
  my $dispatch = {<br>
  'thing_one' => \&thing_one,<br>
  'thing_two' => \&thing_two,<br>
  };<br>
  return($dispatch);<br>
}<br>
<br>
I typically don't have some subs I'm calling but directly calling an<br>
anonymous sub go in there without creating a defined sub.<br>
<br>
I have been using the table something like:<br>
<br>
$key = q(some token or task based on a keyword);<br>
<br>
if ( exists $dispatch->{$key} ) {<br>
  ($some_ref) = $dispatch->{$key}->($key, $some_arg, $another_arg, $baz);<br>
}<br>
<br>
<br>
As near as I can detail I'm not using the same code over again in a copy<br>
and paste fashion.<br>
<br>
The only repeating is the variable name the table is hanging out in and<br>
maybe the key used to call the associated sub ref.<br>
<br>
joe<br>
<br>
_______________________________________________<br>
Purdue-pm mailing list<br>
<a href="mailto:Purdue-pm@pm.org" target="_blank">Purdue-pm@pm.org</a><br>
<a href="https://mail.pm.org/mailman/listinfo/purdue-pm" rel="noreferrer" target="_blank">https://mail.pm.org/mailman/listinfo/purdue-pm</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Dave Jacoby<br>
<a href="mailto:jacoby.david@gmail.com" target="_blank">jacoby.david@gmail.com</a><br>
<br>Don't panic when the crisis is happening, or you won't enjoy it.</div></div></div>