Flow control / case structureby George_Sherston (Vicar)
|on Sep 08, 2001 at 01:58 UTC||Need Help??|
George_Sherston has asked for the
wisdom of the Perl Monks concerning the following question:
I humbly solicit the advice and opinions of brother and sister monks:
My current task is a big CGI script that takes input from two dozen different forms, each of which gives it the information it needs to do a different task. The way I have it set up at the moment is that each form returns a name / value pair in which the name is always "Action" and the value is something memorable and descriptive like "Register", "Book Course" etc (in fact these are taken from the value field of the submit button, which is always named Action).
So my question is, what are the pros and cons of different ways to have my script do the correct task depending on what "Action" is?
I can think of a number of different ways. A tree of if / elsif / elsif... else; a tree of ifs; either of these with code in the loops or else with the loops simply calling the appropriate subroutine; giving my subroutines the same names as the Actions themselves and calling them with, e.g., &$Register... And then I came across this which suggested there must be something called a switch statement that it would be sinful not to use - and I looked it up in Ch 4 of the Camel but I can't see why that would be better than if / elsif. But the fact of the matter is, I don't know.
In any event there seem to a be a shed load of different options for doing this, and I bet there's a lot of accumulated wisdom, and I'd like summa that before I commit. As well as anything else you care to mention I would value views on how these different choices have an effect on:
- readability / maintainability
- speed of execution
- ease of avoiding stupid coding errors.
Also, I should mention that the jury's still out whether this will end up being one file or several.
I'm grateful for any wise words you can offer.
§ George Sherston