Writing code you don't understand two days later is a bad thing. Perhaps you need to comment the code more.
The goal in this case, though, is to learn how to modularize your code effectively. Most CGI programs I write use a dispatch table:
my $thing = $cgi->param('action');
my %actions = (
login => [ \&login ],
main => [ \&view_page, 'front' ],
index => [ \&view_page, 'index' ],
unknown => [ sub { die "unknown action '$thing'" } ],
);
my ($func,@args) = @{ $actions{$thing} or $actions{unknown} };
$func->(@args);
Basically, I set up a hash of function references (and their arguments). Then I call the function corresponding to the action.
Most of my code is broken down into functions in the case of main functionality and repeated functionality -- in other words, the main actions (logging in, viewing a page, searching) are in functions, and common tasks (reading a config file, saving data) are in functions as well.
japhy --
Perl and Regex Hacker
-
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.