[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Why is setup called every time my script is executed?


The way the CGI protocol works (not just cgi::app), the _entire_ script is run for each request. If you're looking for one-time initialization there are options under mod_perl to do this.

Stephen

Brunkhorst, Daniel (DZ-SH) wrote:

Hi!

I thought that possibly time-consuming code that needs to be ( and is
wanted to be ) executed only once goes to method "setup()" and if
something needs to be done even before, it goes to "cgiapp_init()". But
why is it that everytime I call my script with different parameters to a
run-mode, setup() always runs first?

I am using perl 5.8.0, Apache 1.3.27, linux (kernel 2.4.20) and mod_perl
1.27 .

For better readability of this mail the code showing what I do is on the
bottom.

I have a template that has several links that, when being clicked on,
call my script with run-mode "show_objects" and different parameters
like this:

myscript.pl?rm=show_objects;objects=these
or
myscript.pl?rm=show_objects;objects=those

Every time I click on one of those links, "setup()" is called which I
can tell from the debug output from &do_some_lengthy_operations() in the
apache error log and from the 30 seconds it takes to initialize data.

The hash %objects is an "our"-Variable from a module that is used by my
script and it is filled with data by that module. I want to access that
data in my Web-Application many times but create the data only once.
mod_perl or just CGI, I thought this could be achieved by putting that
lengthy stuff into setup method.

Have I misunderstood the way CGI::Application works?

Thanks,
Daniel.


CODE:
-----
package MyApp;

use base 'CGI::Application';
use SomeOtherModule; # this has "our"-hash-variable %objects and fills
it with data
use strict;
use warnings;

sub setup {
   my $self = shift;
   $self->tmpl_path('/path/to/templates/');
   $self->start_mode('start');
   $self->run_modes(
		     start        => 'show_start_page',
		     show_objects => 'show_objects'
		     );
   my $result = &do_some_lengthy_operations(); # taking about 30
seconds
   $self->param( 'store_result', $result );    # store for access in
run_modes
}

sub show_objects {
my $self = shift;
   my $q = $self->query();
   my $which_objects = $q->param( 'objects' );
   my $tmpl = $self->load_tmpl( 'list_of_objects.tmpl' );
   my $output  = '';

   # %objects is an "our"-Variable from a module that is used by my
script
   # and is filled with data I want to access in my Web-Application.
   for my $object ( values %objects ) {
	push @objects, { object_name => $object->{name} };
   }
   $tmpl->param( which_objects => $which_objects );
   $tmpl->param( objects_loop => suppressed );

   $output .= $tmpl->output();

   return $output;
}

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
             http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed




---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
             http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.