Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: How to write long programms?

by japhy (Canon)
on Mar 07, 2001 at 11:39 UTC ( #62688=note: print w/ replies, xml ) Need Help??

in reply to How to write long programms?

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

Comment on Re: How to write long programms?
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://62688]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2014-07-23 18:12 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (149 votes), past polls