How about this variation?:-

#!/usr/bin/perl -w use strict; use warnings; # Set empty strings/arrays my $typed_command = my $cmd_verb = my $cmd_noun = my $action = ""; my @cmd_list = (); # Define subroutines sub misunderstood {print "I don't understand `@cmd_list'\n";} sub go_north {print "You go north\n";} sub go_south {print "You go south\n";} sub go_west {print "You go west\n";} sub go_east {print "You go east\n";} sub get {print "You take the $cmd_noun\n";} sub jump {print "Boing! Boing!\n";} sub quit {print "See you later.\n"; exit;} # Declare valid commands hash table # Pair up the commands with the subroutines that will execute them. # The verb is used as a key in the hash table for quick selection. # This will avoid a whole string of if statements later and keep the # definition near the top of the program for easy editing later. # Don't forget to include synonyms! my %valid_commands = ( 'north', \&go_north, 'n', \&go_north, 'south', \&go_south, 's', \&go_south, 'west', \&go_west, 'w', \&go_west, 'east', \&go_east, 'e', \&go_east, 'get', \&get, 'take', \&get, 'quit', \&quit, 'jump', \&jump, ); # Main loop print "What now?>"; while (defined($typed_command = <STDIN>)) { @cmd_list = split " ", lc($typed_command); $cmd_verb = $cmd_list[0]; $cmd_noun = $cmd_list[1]; if (exists($valid_commands{$cmd_verb})) { $action = $valid_commands{$cmd_verb}; &$action($cmd_noun); } else { &misunderstood(@cmd_list); } print "What now?>"; }

Normally I would have put more whitespace around my code, but I've squashed this up quite a bit so that people don't have to scroll this page so far.


In reply to Re: Naming Subs by Wysardry
in thread Naming Subs by eoin

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.