[cgiapp] CGI::App - creating your own overloaded functions

Brad Van Sickle bvansickle3 at gmail.com
Tue Aug 25 12:17:34 EDT 2009


I find myself duplicating a lot of postrun code, so I'd like to move 
that entire function up to a superclass file to centralize it.
My problem with doing that is that at I still have some logical 
constructs that I need to set somewhere in each modules that are 
module/runmode dependent.
For example:  Javascript includes.  I have several JS include files that 
differ from between modules, and even between runmodes within the same 
module.  So I need some place to build these include strings in each 
module that has access get_current_runnmode. 
As I understand it, I can't reliably ell what runmode I'm in until 
pre-run, so using setup is out.   So I'm attempting to take a concept 
I've read  about for doing something similar in init in the Order of 
Operations document (http://cgi-app.org/index.cgi?OrderOfOperations - 
Solution #2) and apply it to postrun. /

/"In your Super Class module create a function that looks like:

sub app_module_init
{
my $self = shift;/
/# Nothing to init, yet/
}/ /

Then from the cgiapp_init() method in your Super Class module, make a 
call to this new app_module_init() function. Since it's currently empty 
it won't do anything, however, if you create an app_module_init() 
function within your Application module it will overload the one from 
your Super Class. It will now get called whenever cgiapp_init is called 
in the Super Class. If a particular Application module doesn't have an 
app_module_init() method in it, no harm done, the default empty one is 
called in its place."/

So what I've done is created a module in my superclass:*
*sub app_module_define_JS*
*    {*
*    my $self = shift;*
*    return;*
   }*
I then call that module at the start of post run*
my $JSIncludes=&app_module_define_JS;*

As I understand it, if I put an app_module_define_JS sub in a module, 
that function will be overloaded and the code in the module will execute 
instead of the code in superclass.  This doesn't happen.  The code in 
superclass always executes.

Can someone explain why?



More information about the cgiapp mailing list