Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

CGI::Application and Session problem

by Anonymous Monk
on Sep 24, 2003 at 20:40 UTC ( #294009=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I recently began using CGI::Application and HTML::Template to make better CGI apps. I started to add Apache::Session::File and to control session data. I currently have an email app and do not want the message body on the URL line when going to the next run mode. I got Apache Session working but when going to the next run mode a new session is created. It seems that sub Setup is running every time the app is called and I don't think that is right? Does anyone have any thoughts on this? Todd Cox NCI Network Support Rockville, MD
sub setup { my $self = shift; $self->start_mode('mode1'); $self->run_modes( 'mode1' => 'showSelectGroup', 'mode2' => 'showSelectGroupEmail', 'mode3' => 'showComposeMessage', 'mode4' => 'showConfirm' ); $self->tmpl_path("c:/Apache/htdocs/ncimail/"); my @prop; # Store the properties in a file so it is easily changed. open(PROP,"c:/apache/prop/") || die "Can't open l for reading!"; while(<PROP>) { chomp; push(@prop, $_); } close(PROP); my $hostname = $prop[4]; my $search_dn = $prop[5]; my $search_pwd = $prop[6]; my $scope = $prop[7]; my $port = $prop[8]; my $ldap = Net::LDAPS->new($hostname,port => $port,timeout => TI +MEOUT) or die "Can't connect to LDAP server " . $hostname; my $result = $ldap->bind($search_dn, password => $search_pwd, ve +rsion => 3); my $error = $result->error(); if($error ne "Success") { print ("<BR>LDAP Bind Error: $error<br>"); exit; } my %session; my $id; tie %session, 'Apache::Session::File', $id, {Directory => 'c:/ap +ache/tmp/sessions', LockDirectory => 'c:/apache/tmp/lock'}; my $session_id = $session{_session_id}; $self->param('session_id' => $session_id); }

Replies are listed 'Best First'.
by PodMaster (Abbot) on Sep 24, 2003 at 22:51 UTC
    It seems that sub Setup is running every time the app is called and I don't think that is right?
    That's what happens, and that's what's supposed to happen (every time you construct a new CGI::Application object, sub new calls setup). Try this from the commandline (or just run perldoc on it)
    package CGI::Application::Loop; use base qw[ CGI::Application ]; 1; package main; unless( caller() ) { use Devel::TraceCalls { Package => "CGI::Application" }; my $app = CGI::Application::Loop->new(); print STDERR "\nTRACE: CONSTRUCTED**(\n"; $app->run; print STDERR "\nTRACE: RUNNED**(\n"; $app->run; } __END__ perl >NUL 2>twoy perl -lne" print qq[=head3 $1\n\n] if m{^TRACE:[^\w]+(.*?)\(} " twoy > +> =head1 What is the application loop? Well, first you create an object of your CGI::Application subclass (or + CGI::Application). You use L<I<C<new>>|/CGI::Application::new> for this. After you have created an instance of your application ( C<my $app = CGI::Application-E<gt>new> ), you run it (L<C<$app-E<gt>run>|/CGI::Application::run>), and that's it (L<I<C<run>>|/CGI::Application::run> has a loop of its own). At that point your CGI::Application object should go out of scope (or be undef'ed) and subsequently be DESTROY'ed. =head2 CGI::Application::new The constructor. It sets defaults for the following: =over 4 =item * C<$self-E<gt>header_type> =item * C<$self-E<gt>mode_param> =item * C<$self-E<gt>start_mode> =back It then calls L<C<$self-E<gt>cgiapp_init>|"CGI::Application::cgiapp_init">, and L<C<$self-E<gt>setup>|"CGI::Application::setup">. =head3 CGI::Application::cgiapp_init This one I<you> must provide. It's a good place to initialize a database handle. =head3 CGI::Application::setup This one also I<you> must provide, and it should set C<$self-E<gt>start_mode>, and C<$self-E<gt>run_modes> =head2 CGI::Application::run The C<run> method is the I<meat'n'potatoes> of CGI::Application. It fetches the L<query|/CGI::Application::query> object. It fetches the mode_param, and the current mode, and if there is no current run mode specified, it fetches the start_mode. It then invokes L<cgiapp_prerun|/CGI::Application::cgiapp_prerun>. It then invokes the actual run mode (whatever it may be, start_mode or + otherwise). It then I<builds> the headers using C<header_type()> and C<header_prop +s()>. It then sends "I<output to browser (unless we're in serious debug mode +!)>". And it finally calls L<C<$self-E<gt>teardown()>|/CGI::Application::tea +rdown>. =head3 CGI::Application::query Returns the query object ;)( usually a C<> object, but you can just as easily use a completely B<compatible> C<CGI::Simple> object). If one doesn't exist, one will be created using C<$self-E<gt>cgiapp_get_query>. Creates a object on the fly. #=head3 CGI::Application::mode_param #=head3 CGI::Application::start_mode =head3 CGI::Application::cgiapp_prerun This one you must provide. It is a good place to fetch a user object from someplace. =head3 CGI::Application::teardown This is a good place to destroy your database handle, unless you're running in a persistent environment (aka unless C<$ENV{MOD_PERL +}>). Since it is invoked upon each I<L<run|/CGI::Application::run>>, it is a good place to delete I<user> objects and such. =cut
    Here is the raw trace (what you would see if you ran this code)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: CGI::Application and Session problem
by Anonymous Monk on Sep 25, 2003 at 12:58 UTC
    I get your point and didn't realize that that was the case, but it is very obvious! So I still need to find a way of keeping track of the data a user enters without passing it back and forth on the URL. The one thing that doesn't make sense is that in the CGI::Application docs it talks about setting up a database handle but in reality it sets up every time so what is the benefit? I am missing a real big picture here.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://294009]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2017-09-20 17:38 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (237 votes). Check out past polls.