[Tutor] Perl refugee

Brian Wisti brian@coolnamehere.com
Sat, 27 Apr 2002 21:46:41 +0000


Hi all,

Erik Price writes:
 > 
 > On Friday, April 26, 2002, at 10:38  PM, dman wrote:
 > 
 > > IME switch statements are rarely used (even in C/C++/Java) anyways,
 > > and in the situations where it might be useful a dictionary lookup can
 > > be much more elegant.
 > 
 > I use them religiously in PHP for web site development.  Often a PHP 
 > "script" will have more than one "instantiation".  That is to say, it 
 > displays a different page (from the user's perspective) depending on 
 > what data it is working with.  Usually I use an HTML hidden form field 
 > to send a variable named "action" which could do something like this 
 > (pseudocode based on Python):
 > 

Ooh, ooh, I know this one! ... Sorry, I don't really know where that
came from.  Not really sure if anybody would want to know this, but
that doesn't stop me ;-)

Anyhow, there's an alternate approach to the type of situation you
describe.

1. Create a "common_page" function which returns a site-standardized
page with provided content. An "error_page" might be a good idea, too.

-----
def common_page(title, content):
    # ... Insert the title and content into a template page ...
    return output

def error_page(error, help_text=""):
    # Turn the error and help_text into a helpful error message
    return common_page("Error", content)
-----

2. Create a series of action functions ("new_user", "profile_edit_view",
"profile_save", etc) which build some content based on cgi's form
values, and return the content inserted into the common_page function.

-----
def profile_save():
    # ... Do some processing
    # Generate some content and a title based on the processing
    return common_page("Your Profile Has Been Saved", content)
-----

3. Create an actions dictionary.  The keys are legitimate action
names, while the values point to action methods.  Like so ...

-----
actions = {
	'new': new_user,
	'edit': profile_edit_view,
	'save': profile_save,
	'default': front_page
}
-----

4. In your actual form processing code, figure out which action is
required and call the appropriate function.  Kinda like this:

-----
form = cgi.FieldStorage()
action = "default"
output = None

if form.has_key('action'):
   action = form['action'].value

try:
   output = actions[action]()
except Exception, e:
   output = error_page(e, "Please try again later.")

print headers
print output

-----

This is just off the top of my head, so it's missing nearly all of the
errer cheking that would normally be there.  The point of all this is
that ... uhh ... oh yeah - it hides the processing away in functions
(if there is common processing between two functions, then it's time
for some refactoring!), it provides case-like functionality without
worrying about the break statement, and it's a technique that works
equally well for me in Python, Ruby, or Perl.  Actually, the idea
occurred to me while trying to get something like a case statement
under Perl.

I've gotten quite used to it, and use case statements much less
frequently nowadays (in languages that support them, of course).

A co-worker once told me that this approach has an actual name with
big words attached to it.  I am a Geek of Very Small Brain, however,
and that name has been lost in a fog of caffeine consumption and
coding frenzy.  Sorry about that.  Maybe a real programmer in here 
can tell me what it's called. Besides "very silly."

Okay, no more reading email after the 3rd pot of coffee.  I'm going to
school this Fall so I can find out the names of all the tricks I've
learned over the years :-)

Later,
Brian Wisti
brian@coolnamehere.com
http://www.coolnamehere.com/