Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://62688]
[1nickt]: Config works properly, thank you. I has first followed an example from the doc using eval. THis is much nicer.

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-10-18 15:41 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (249 votes). Check out past polls.