Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2018-01-20 17:47 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.