On Sunday, March 31, 2002, at 07:07 , Jonathan E. Paton wrote: [..] I had not thought of the SWITCH case being one way or the other in the OO v. Proceduralists approach - but your proposition does present the problem - what is the tradeoff between 'runtime performance' and maintainability. >> this way as the @state list grows or shrinks the one simply >> adds in one more switch statement.... > > What if there is HUNDREDS of items, then it'd be really slow. For a > better approach using hashes then see what I did in: > > http://groups.yahoo.com/group/perl-beginner/message/9583 OOOOOOO!!!!! SLICK!!!!!!!! { test code below } The question that now comes to mind is why not put all of the 'response functions' into a Perl Module - so that one can maintain it independent of the 'usage' in the script? At which point we do get to the 'OO v. Proceduralist' Issues since the simple 'new' function of sub new { my ($type,$string) = @_ ; my $self = {}; my $class = ref($type) || $type ; bless $self, $class ; } really is little more than returning a 'hash' to begin with... OR am I diving off the deep end into a place we should go on some other list??? #------------ Ok... the following is a test case piece of code.... vladimir: 103:] perl hashSwitch.pl MA CI bob DE No such function do_DE No such function do_IN MA_response CI_response invalid state info : bob invalid state info : DE vladimir: 104:] ### #!/usr/bin/perl ### ### use strict; ### ### # http://groups.yahoo.com/group/perl-beginner/message/9583 ### # testing Jonathan E. Paton's theory ### ### #fill in 11 other states ### my @state = qw(MA CI DE IN OH); ### ### my $response=''; ### ### my %lookup_func = build_lookup(@state); ### ### foreach my $statefield (@ARGV) { ### ### if ( exists($lookup_func{$statefield})) { ### $response = $lookup_func{$statefield}->() ; ### ### print "$response \n"; ### } else { ### print "invalid state info : $statefield \n"; ### } ### ### }# end foreach ### ### exit(0); ### #----------------------------- ### # the response cases ### ### sub do_MA { my $string = "MA_response"; $string; } ### ### sub do_CI { my $string = "CI_response"; $string; } ### ### sub do_OH { my $string = "OH_response"; $string; } ### ### sub build_lookup { ### ### my (@actions) = @_; ### ### my %lookup; ### ### foreach my $func (@actions) { ### my $funName= "do_${func}"; ### if ( defined(&{$funName}) ) { ### $lookup{ $func } = \&{$funName}; ### } else { ### print "No such function $funName\n"; ### } ### } ### ### %lookup; ### } ok, so the build_lookup() is a skank to simplify the typing. ciao drieux ---Thread Previous | Thread Next