
Jasmine
Administrator
Jan 25, 2000, 1:09 PM
Post #12 of 15
(13795 views)
|
Personally, I don't like the if ... elsif ... else statements myself. I find it really cumbersome for very large programs. My lastest kick is using references to manage what action the program should take based on form input. For example, one action could be to display "introductory" or "help" information, another could be to display user options. Since I wanted each section to have different headings, suboptions, etc., I found that using subroutine and array references was the easiest way to do this. Here's how I use references to make my life easy By creating an array for section elements, individual subroutines for each section, and creating a hash that associates the array and subroutine to an individual section, I was able to easily manage a large program as well as add and delete sections and section elements as needed. This way, by simply passing an "action=1", the program checks the hash for the appropriate subroutine and array to use. (I prefer using digits because it lends to greater flexibility (eg $FORM{'action'}++ So first, in a configuration file, I defined arrays that I want to be specific for each section: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> @main = ("Home Page","Instructions"); @accounting_setup = ("Departments","Top Level Categories","Chart of Accounts"); </pre><HR></BLOCKQUOTE> Then, beneath that, I created a hash that accociates the page heading, array, and subroutine (added in the next step) with an single numeric key: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> %accountmain = ( 0 => ["Main",\@main,\&intromain], 1 => ["Accounting Setup",\@accounting_setup,\&introaccounting], ); </pre><HR></BLOCKQUOTE> Then, in the body of the program, I use the following line (after calling the read input subroutine): <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> if (defined($accountmain{$FORM{'action'}}[2])){ &{$accountmain{$FORM{'action'}}[2]}; } else { print "Subroutine for $FORM{'action'} doesn't exist.<P> The defined sections are:<BLOCKQUOTE>"; @acct = sort {$a <=> $b} keys %accountmain; foreach (@acct){ print "$accountmain{$_}[0]<BR>\n"; } print "</BLOCKQUOTE>"; } </pre><HR></BLOCKQUOTE> Basically, the if block checks to see if the key (which was passed to the program) exists for %accountmain. If it does, it invokes the subroutine that's referenced in %accountmain and the subroutine knows which array to use. You'll just have to be careful (as usual) that the referenced subroutine exists. The subs look like: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> sub intromain { print qq~ Hello - in $accountmain{$FORM{'action'}}[0]<BR> <BLOCKQUOTE><FORM><SELECT NAME="blah"> ~; foreach (@{$accountmain{$FORM{'action'}}[1]}){ print "<OPTION>$_<BR>"; } print "</SELECT></FORM></BLOCKQUOTE>"; } </pre><HR></BLOCKQUOTE> You're still invoking a normal subroutine, but need a special format to access the heading and arrays. Heading (the first element in the section's hash) needs to be accessed using $accountmain{$FORM{'action'}}[0]. The array (the second element in the section's hash needs to be accessed using @{$accountmain{$FORM{'action'}}[1]} You can see this in action... First section, Second section and Third section (undefined action) If any of this is confusing, please let me know and I'll be glad to elaborate. [This message has been edited by Jasmine (edited 01-25-2000).]
|