Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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?

Replies are listed 'Best First'.
Re^2: Coding for maintainability
by planetscape (Chancellor) 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?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2019-11-18 11:26 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (89 votes). Check out past polls.