Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Coding for maintainability

by dragonchild (Archbishop)
on Feb 16, 2006 at 18:07 UTC ( #530722=note: print w/ replies, xml ) Need Help??


in reply to Coding for maintainability

You need a modified dispatch table. A dispatch table is where you have (generally) a hash of names where the values are references to subroutines.

my %dispatch = ( foo => sub { print "foo\n"; }, bar => \&bar, ); chomp( my $input = <> ); unless ( exists $dispatch{ $input } ) { die "I don't know what to do with '$input'\n"; } $dispatch{$input}->(); sub bar { print "bar\n"; }
The modification needed is that you aren't matching on a simple string. There's a few ways to improve this. One is to pass $family to every function and let them determine if they want to handle it. Then, the function returns either true (I handled it) or false (I don't deal with this value). Errors would be propagated with die and caught with an eval-block.

Then, whenever you add something, you're working with a much smaller piece of the puzzle because the engine and the parts are separated.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?


Comment on Re: Coding for maintainability
Download Code
Re^2: Coding for maintainability
by planetscape (Canon) on Feb 17, 2006 at 07:15 UTC
      Hello, Currently I have used the code provided by jdporter instead of my switch statement. However, I will now look at the dispatch table option that you have recommended and see if that will make this script easier to maintain. Thanks for your reply

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://530722]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-12-27 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls