joshua has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm starting a project that will probably take me the whole summer to complete.

Anyway, I'm starting by making the administration part of the site. I need it to do different tasks like change password, add new users, delete users, etc. Would it be better to have these tasks incorporated in the same script or to have them run with multiple scripts?

If I use the same script, should I seperate these tasks into subs? Or, should I use ifs and elsifs to tell the script what to do? E.g. ...

my $action = param('action'); if ($action eq 'foo') { # Do stuff } elsif ($action eq 'bar') { # Do stuff }

Or...

if ($action eq 'foo') { foo(); } elsif ($action eq 'bar') { bar(); } sub foo { # Do stuff } sub bar { # Do stuff }
Thanks for any input.
Joshua

Replies are listed 'Best First'.
(crazyinsomniac) Re: Multiple tasks in same_script, or multiple_scripts?
by crazyinsomniac (Prior) on Jun 02, 2002 at 03:11 UTC
Re: Multiple tasks in same_script, or multiple_scripts?
by mfriedman (Monk) on Jun 02, 2002 at 03:54 UTC
    If you're going to one-script route, and you don't want anything fancy like CGI::Application, I would definately reccomend AGAINST using a giant if/else structure for your script. Maintaining code like that can be a nightmare. (Trust me, I know. One developer at my company left behind a webapp with a 1000 line if/else structure. Yikes.)

    I prefer using a hash of sub references as a dispatch table.

Re: Multiple tasks in same_script, or multiple_scripts?
by rbc (Curate) on Jun 02, 2002 at 05:20 UTC
    You might want to be able to complete each task in
    multiple scripts. Or better yet multiple object orientated
    perl modules. For example you could have an User.pm object.
    package User; use strict; ... sub new { my ($pkg,$pw, $userName, $home,$shell) = @_; bless { password => $pw || "Changeme", name => $userName, home => $home || "/usr/home/$userName", shell => $shell || "/usr/bin/bash" }, $pkg; } sub changePasswd { my $obj = shift; my $oldPassword = shift; my $newPassword = shift; ... $obj->{password} = newPassword; } ... 1;
    Then you could combine these scripts into one or more
    scripts if you want. Heck why not turn them into "Web
    servers" with SOAP::Lite!

    And yes you should use subs and try to avoid big ugly
    if elsif statements as other monks have pointed out.

    Have fun!
Re: Multiple tasks in same_script, or multiple_scripts?
by r0b (Pilgrim) on Jun 02, 2002 at 08:54 UTC
    I would definitely recommend splitting your application into lots of smaller scripts. For example you could have a script for each task e.g. adduser.pl deluser.pl etc.

    I have tried both approaches to big web applications and I have found that the small script approach is better in two main respects: maintainability and speed.

    Good luck with your project!

    ~~r0b

Re: Multiple tasks in same_script, or multiple_scripts?
by 5-7-5 (Initiate) on Jun 02, 2002 at 04:46 UTC
    I wanted to use
    good development framework
    XML::Comma

    *the haikunator*
Re: Multiple tasks in same_script, or multiple_scripts?
by Aristotle (Chancellor) on Jun 02, 2002 at 14:39 UTC
    Your question is fairly vague, so I'll recommend you put the common code in modules even if you do it all in a single script and leave it at that. Separate the logic from the frontend and they'll both be more maintainable. After that, the decision between a single do-all script and multiple specialized ones becomes more of a question of preference and possibly requirements.

    Makeshifts last the longest.

Re: Multiple tasks in same_script, or multiple_scripts?
by joshua (Pilgrim) on Jun 03, 2002 at 16:24 UTC
    As always, you all's input has been most helpful. I'll plan to try the different techniques posted, and I'll use the one I like the most. I'll probably be asking more questions in the near future and appreciate everyone's help.

    Thanks!
    Joshua