[Tutor] Perl refugee
Erik Price
erikprice@mac.com
Sat, 27 Apr 2002 21:06:31 -0400
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):
# this is a form that accepts user input and enters it into a database
switch(action):
case 'commit':
commit data to database (execute query)
print success message to user
break
case 'confirm':
print a confirmation message with yes/no button
including a hidden form field sending
POST variable 'action=commit'
break
case 'display_form':
print an HTML form to accept user input
including a hidden form field sending POST
variable 'action=confirm'
break
default:
print instructions for user
button to send POST variable 'action=display_form'
Of course, they get a bit more complex than that, but that's why I like
switch() -- because sometimes I want the user to cascade down into the
next case statement to do some other thing, so I don't have a "break" at
the end of the case that picked up the "action" variable. Anyway, it's
a nice feature, but since I haven't yet done any real dynamic HTML with
Python I haven't really missed.
and Python more than makes up for it by being more fully object-oriented.
Erik