Hello Everyone:

I have recently been updating my CGI::Application program and it has grown enormous (for me at least), coming in at just shy of 3000 lines. Since it is just one file it is becoming a PITA to maintain. I am using around 20 perl modules (a lot of them are CGI::App::Plugins) and have a lot of various run modes.

I would like to break up the script into several parts so that I am not loading a lot of unnecssary modules and reading all those functions each time a webpage is loaded. However, I have not written a program this large that I need to break it up before. What my idea would be to have a main file with all the various run modes. Then in the run mode function it would load the modules that contains all that functionality (via require?). In pseudo code I want to do this:

package MyCGIApp; use base 'CGI::Application'; use CGI::Application::Plugin::AutoRunmode; sub setup { ... load database ... ... load session ... ... other setup stuff... } sub runmode1 : Runmode { my $html_output = ''; $html_output .= $q->start_html(-title=>'petnuch.com', -style=>{-src=>'/css/layout.css'}); $html_output .= $self->banner; $html_output .= $self->menu; require 'MyCGIApp::Runmode1'; # do_runmode1 is defined in MyCGIApp::Runmode1 $html_output .= $self->do_runmode1; $html_ouput .= $self->footer; return $html_output; } /* Common functions to every web page */ sub banner { ... } sub menu { ... } sub footer { ... } 1;

But what do I need to put in Runmode1.pm? Specifically what do need to do with ISA. Is it a CGI::Application or a MyCGIApp. Do I need to export do_runmode1? Do I need to have a 'use CGI::Application::Plugin::Session' in each runmode modules, or will it automatically be loaded? This is what I have in pseudocode (that does not work):

package MyCGIApp::Runmode1; use strict; use Runmode1specficmodules; sub do_runmode1 { my $self = shift; my $session = $self->session; my $q = $self->query(); my $id = $q->param('n'); unless( somecondition ) return $self->error; ...do stuff... return $html_output; } sub error { ...runmode1 error stuff... } 1;

All help will be greatly appreciated!


In reply to Breaking up a CGI::Application program by debiandude

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.