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

Organising big CGI::Application codebases

by zby (Vicar)
on Sep 05, 2008 at 09:37 UTC ( [id://709213]=perlquestion: print w/replies, xml ) Need Help??

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

Dear fellow monks,

The whole application is just one object and all runmodes need to be it's methods. Is that right?

If it is - then how do you divide the code into packages?

  • Comment on Organising big CGI::Application codebases

Replies are listed 'Best First'.
Re: Organising big CGI::Application codebases
by scorpio17 (Canon) on Sep 05, 2008 at 13:53 UTC

    When you use CGI::Application (C:A), you have a small instance script that creates an object (MyApp, for example) which inherits from the C:A base class. The package file that defines MyApp contains "run modes" (methods) that correspond to your web pages.

    The problem one may have, is that if your web app grows to be large and complicated (and don't they all?) you may find that your MyApp package contains dozens of methods. All of these have to be read, loaded, compiled, etc. every time, for every page view.

    The unwritten assumption in the C:A docs (in my opinion) is that, ideally, you'll be running your app under mod_perl. Because if you use mod_perl, all of your methods get loaded, compiled, etc. when the web server first starts up. It's a one time cost that way. The down side is that it takes a lot more memory, but that's what mod_perl does: it lets you buy faster performance in exchange for larger memory usage.

    If you'r stuck running in regular mod_cgi mode, you still have options, but you have to really think about what makes sense for your particular app. Some people break large projects up into multiple smaller projects, each having its own instance script. You can factor out all the common stuff, to avoid redundancy. So you may have objects MyApp1, MyApp2, MyApp3, etc. (each with their own instance scripts), each of which inherit from object CommonStuff, which inherits from C:A. This helps MyApp1 avoid having to load stuff used exclusively by MyApp2, etc. But you can wind up having dozens of little scripts this way, instead of just one big one, which is one thing C:A tries to avoid. C:A:Dispatch can be used in this case, as a instance script "factory" which let's you avoid having multiple, nearly identical instance scripts, but you'll still have lots of package (.pm) files.

    Another thing you can do is break out methods into seperate packages, and then "require" them in the methods that need them. This give you a "load on demand" system (rather than loading everything up front with "use").

    But if your site is going to be low traffic / low volume, you may not actually see any performance problems using the "one big file / mod_cgi" approach. The problem is that this approach doesn't scale very well as traffic increases into millions of hits per day. But that's a problem most web developers only ever dream of having. Even then, sometimes the easiest solution is buyings a bigger server with more memory. There's no one "right" solution, in general, you just need to list of the pros and cons of each approach with respect to your project and decides what's right for you.

      Thanks. I see I was not precise enough. What I have here is a legacy code - it is one huge class (i.e. package) split into many files for easier editing. This splitting breaks the usual assumption about the correspondence between the file name and the package contained in it. I guess I can live with that if I have - but it would nice to at least organise my new code in a more intuitive (i.e. standard) way. I am now looking into C:A:Dispatch - it is promising.
Re: Organising big CGI::Application codebases
by rhesa (Vicar) on Sep 05, 2008 at 11:31 UTC
    I recommend CGI::Application::Dispatch. It's by far the simplest and most scalable way to structure big applications into separate and independent modules. Each module would be a separate CGI::Application subclass.
Re: Organising big CGI::Application codebases
by themage (Friar) on Sep 05, 2008 at 10:13 UTC
    zby,

    I'm not sure what you mean when you say that your whole application is just one object and the runmodes it's methods. But I will agree with you for the moment. And illustrate what I think is your point.

    I like the main code for my bigger applications to be:
    use App::Class; App::Class->new()->run();
    And then, inside my application any external interface with the application is done calling App::Class methods. That means, obviously, that all my run modes are methods of my class.

    But I will not write all the code for my application. I'll use code others (or myself) did writen in other instances. Code for specific functions.

    For instance, I'll use a module I had written that extends Config::Tiny. See the readconfig for App::Class:
    use Config::myTiny; sub readconfig { my $self=shift; my $cfg=$0; $cfg=~s/\.pl$/.ini/; my $conf=Config::myTiny->read($cfg); $conf=Config::myTiny->new() unless $conf; $self->{config}=$conf; }
    Now, whener I need to get a config variable, I'll use $self->{config} like this:
    sub run { my $self=shift; $self->readconfig(); # ... if ($self->{config}->get('options','showonstartup')) { $self->show_mainwindow(); } # ... }


    So, you application will be a single class, but you should break your code to create smaller complete and independent objects, that you will use in the code for your main application.

    OoP allow you other things also. If you have (or intent to have) several application that use identical functionalities as their base, you can create a base class that implement those functions and extend that using it as base for every other application.

    I think that this are the most simple and easier ways to use OoP that can help you reduce your codebase, improve your code, as the code you reuse frequently will tend be corrected faster (assuming you use the same code, not that you copy it from project to project), and get your applications running faster.

    Remember...
    • use base qw(Your::Base::App);
    • if you can isolate something, you can turn it into an object


Re: Organising big CGI::Application codebases
by dragonchild (Archbishop) on Sep 06, 2008 at 02:22 UTC
    I've written about this several times in the past. Super Search is your friend.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://709213]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found