On Thursday, July 11, 2002, at 07:17 , Jason Frisvold wrote:
> Does this do what I think it does? Namely, converts the == or eq to the
> proper operator based on the type of call being made?
jason thought I would toss this back at the list...
[..]
I presume you are asking about:
>> or adopt a simple hash_of_sub strategy like:
>>
>> ### if ( exists($lookup_func{$SiteType})) {
>> ### $response = $lookup_func{$SiteType}->() ;
>> ### } else {
>> ### $response = $lookup_func{'default_function'}->() ;
>> ### print "invalid state info : $statefield \n";
>> ### }
>> ###
>> ### print "$response \n";
>>
>> cf:
>> http://www.wetware.com/drieux/pbl/misc/HashSwitch.txt
in a twisted sense of the term - it 'avoids' the discussion
of the "==" XOR 'eq' for if/elsif/else structures by going
at the problem differently....
we start with the simple process as the OP outlined:
if ($SiteType eq "PHP3") {
do some stuff
} elsif ($SiteType eq "PHP4") {
do different stuff
} elsif ($SiteType eq "ASP") {
do different stuff
....
} else {
do default stuff
}
As long as the 'comparison' is with a 'token' that is
simple enough to be a 'key' - why not evolve into a
general abstract of the form:
if ($key eq 'foo' )
do_foo(@arglist);
elif ($key eq 'bar' )
do_bar(@arglist);
elif ($key eq 'baz' )
do_baz(@arglist);
else
do_bob(@arglist);
Which is really not a hard leap to make - since the original
format basically implies it - since, well, anything between
a set of curly braces is a sequence of script language commands,
and a sub is really little more than a named sequence of
script commands - with the potential to
a) take in arguments
b) return a sequence of things
Ok, so I did shift to the more obvious '$key' and to the
non-interpolating single quote around our single tokens
in the abstraction - in part to lean towards,
Hey kids, what if we were to stuff this into a hash?
since I already had a proof of concept piece of code
laying around that does:
### my %lookup_func = build_lookup(@state);
###
### ....
###
### if ( exists($lookup_func{$statefield})) {
### $response = $lookup_func{$statefield}->() ;
###
### print "$response \n";
### } else {
### print "invalid state info : $statefield \n";
### }
###
I would of course change the 'build_lookup()' and some
of the gory details along the way....
But it would appear to me to be simpler to maintain
in the long run - since that 'chunk' of code does not
need to be 're-written' every time we have to add a
new type of thing to deal with....
a) we prove that the given stack of scripting language
commands does what we want - start small, make a
simple script...
b) we wrap that as a new sub in the game
c) we get build_lookup to return the new entry
in our %hash and we are outta there....
The next round of escalation of course is to resolve
whether this 'stack of subs' needs to be pulled out
of this script and stuffed into
a) a perl library we suck in with require
b) a perl module we import the functions
c) a perl module in an OO style in which we
my $lookup_func = new FOO::BAR;
if ( $lookup_func->query_for_function($SITETYPE) ) {
my $response = $lookup_func->{$SITETYPE}->(@arglist);
} else {
my $response = $lookup_func->{'default'}->(@arglist);
}
or for the 'flatlanders'
my $response = ( $lookup_func->query_for_function($SITETYPE) ) ?
$lookup_func->{$SITETYPE}->(@arglist) :
$lookup_func->{'default'}->(@arglist);
which could have been cured by abstracting that sequence into
the module itself as
my $response = $lookup_func->get_response($SITETYPE,@arglist);
and totally do the code maintenance external to this script
in the release of the supporting module.....
It's all really a matter of when, where and how do you
want to do your code maintenance, and to what level of abstraction....
HTH...
ciao
drieux
---
No implementation of code ever survives
the encounter with the external world....
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]