Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
One structure you may want to consider is a State Machine. Now, I am sure CS and algorithm purists will have a far better definition that what I have here, but I will make an attempt.
  1. Your program consists of a single loop, consisting of what is really a big CASE statement (or SELECT, depending on your religion). You start with one of several initial states, based on your initial task.
  2. Each CASE / SELECT entry (just use an if test like if (&mystate eq 'SomeValue') will do some processing or validation that is required at that step, and then makes a decision on what the next state should be.
  3. The loop continues until you reach a "completed" state.
So, you initial state could well be set by the value of your submit button, and you can then detrermine what the correct next state shuld be. For example, assume the initial state is "REGISTER". The fragment of code may be:
if (&mystate eq "REGISTER") { if (&UserName eq "") { &mystate = "ERROR"; &error_code = "Missing user name"; } else { &mystate = "CHECKADDRESS"; } }
and so on. Now, several of your forms may want to perform the same processing - for example, checking a form value that could be on several forms. So the decision for the new value of &mystate could depend on what the initial request was.

Percentive monks will see that by using a sequence of IF blocks, within one pass of the loop, the program may pass through several states in sequence - that is not a problem. That advantage of this approach is that you have broken up your processing logic into a number of discrete chunks, each doing a particular function (validating an address, doing a data base search for a list of courses, whatever), and hence becomes easier to maintain.

As well, your business logic - what is required to process a course booking, for example, is listed in the logic of chosing the state progressions. So if you need ot add some additional business logic at a later stage (oops - we forgot that we need an email address when accepting course bookings), then you can add it reasonable simply.

I apologise if the explanation is a trifle obscure - one should not try to undertake algorithm design late on a friday evening!


In reply to Re: Flow control / case structure by Maclir
in thread Flow control / case structure by George_Sherston

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found