For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > November 2004 > CGI::Application design question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author CGI::Application design question
Kasturirangan Rangaswamy

2004-11-27, 8:55 am

Hi,

This is a CGI::Application design question. It concerns side-
stepping normal run modes at any point within the application
to display an error screen.

I have created a 'base' module whose job it is to

a. Setup a connection to a database
b. Initialize session
c. Bring the user to an index screen.

I have put (a) and (b) in the cgiapp_init method as I intend to
extend this class in the future.

My code inside cgiapp_init looks as below


---------------------------------------------------------------------
sub cgiapp_init {

my $self = shift;

#Create a database connection
$self->initialize_database;

#Create a new session
$self->initialize_session;

}

---------------------------------------------------------------------

'initialize_database' is a subroutine which is as follows


---------------------------------------------------------------------
sub initialize_database {

open(DB_CONNECTION_FILE, '/export/home/r/rangask/software/cgi-
bin/SoftwareDistribution/common/daase_connection_params.txt') ||
display_error_page("blah");
my ($hostname, $database, $user, $password) =
split(/:/,<DB_CONNECTION_FILE> );

}

---------------------------------------------------------------------

I wish to display an error screen if something goes wrong with
opening of the file. Thats done by 'display_error_page' subroutine.


---------------------------------------------------------------------
sub display_error_page {

#my $self = shift;
print "in here";
my $msg_string = shift;

#Load the error template page
# my $tmpl_error_page = $self->load_tmpl('error_page.tmpl',
# die_on_bad_params => 0,
# cache => 1
# );

my $tmpl_error_page = HTML::Template->new(filename =>
'/export/home/r/rangask/software/html/error_page.tmpl');
$tmpl_error_page->param(error_message => $msg_string);

$tmpl_error_page->output();
print "after the template";

}

---------------------------------------------------------------------

Finally, my setup method is as follows


---------------------------------------------------------------------
sub setup {

my $self = shift;

#Note that no tmpl_path is being used here. We will give that
while calling this module

$self->start_mode('display_entry_screen');
$self->run_modes(
'display_entry_screen' => 'display_entry_screen',
'AUTOLOAD' => \&deal_with_undefined_run_mode
);
}

---------------------------------------------------------------------

As you can see, I am calling the 'display_error_page' subroutine
from within the 'initialize_database' subroutine. So before I display
any page, if the file-open fails, I want to display an error page.

PROBLEM: I get to the error subroutine (I should because the DB
parameter filename given is wrong and I tested this fact by giving some

print statements and observing the command line output) but am
automatically redirected to the main page through the
'display_entry_screen' subroutine (details of that sub not shown
here)

How do I make it stay on the error page?

I could possibly define the 'display_error_page' as a run mode and
use the cgiapp_prerun and the mode_prerun methods to go to it
but this 'display_error_page' is a common method I want to call from
my 'child' modules and I feel defining it as an additional run mode
will prove restrictive.

I guess my question could also be rephrased as:

Is there a way of breaking the run-mode path of a CGI::Application
to throw an error page?

Any help is appreciated. Thanks for your patience in reading this
long post.

Sharad



__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail
krmair01i@ozemail.com.au

2004-11-27, 8:56 pm


>
> From: Kasturirangan Rangaswamy <kasturiranganr@yahoo.com>
> Date: 27/11/2004 16:54:19
> To: beginners-cgi@perl.org
> Subject: CGI::Application design question
>
> Hi,
>
> This is a CGI::Application design question. It concerns side-
> stepping normal run modes at any point within the application
> to display an error screen.
>
> I have created a 'base' module whose job it is to
>
> a. Setup a connection to a database
> b. Initialize session
> c. Bring the user to an index screen.
>
> I have put (a) and (b) in the cgiapp_init method as I intend to
> extend this class in the future.
>
> My code inside cgiapp_init looks as below
>
>
> ---------------------------------------------------------------------
> sub cgiapp_init {
>
> my $self = shift;
>
> #Create a database connection
> $self->initialize_database;
>
> #Create a new session
> $self->initialize_session;
>
> }
>
> ---------------------------------------------------------------------
>
> 'initialize_database' is a subroutine which is as follows
>
>
> ---------------------------------------------------------------------
> sub initialize_database {
>
> open(DB_CONNECTION_FILE, '/export/home/r/rangask/software/cgi-
> bin/SoftwareDistribution/common/daase_connection_params.txt') ||
> display_error_page("blah");
> my ($hostname, $database, $user, $password) =
> split(/:/,<DB_CONNECTION_FILE> );
>
> }
>
> ---------------------------------------------------------------------
>
> I wish to display an error screen if something goes wrong with
> opening of the file. Thats done by 'display_error_page' subroutine.
>
>
> ---------------------------------------------------------------------
> sub display_error_page {
>
> #my $self = shift;
> print "in here";
> my $msg_string = shift;
>
> #Load the error template page
> # my $tmpl_error_page = $self->load_tmpl('error_page.tmpl',
> # die_on_bad_params => 0,
> # cache => 1
> # );
>
> my $tmpl_error_page = HTML::Template->new(filename =>
> '/export/home/r/rangask/software/html/error_page.tmpl');
> $tmpl_error_page->param(error_message => $msg_string);
>
> $tmpl_error_page->output();
> print "after the template";
>
> }
>
> ---------------------------------------------------------------------
>
> Finally, my setup method is as follows
>
>
> ---------------------------------------------------------------------
> sub setup {
>
> my $self = shift;
>
> #Note that no tmpl_path is being used here. We will give that
> while calling this module
>
> $self->start_mode('display_entry_screen');
> $self->run_modes(
> 'display_entry_screen' => 'display_entry_screen',
> 'AUTOLOAD' => \&deal_with_undefined_run_mode
> );
> }
>
> ---------------------------------------------------------------------
>
> As you can see, I am calling the 'display_error_page' subroutine
> from within the 'initialize_database' subroutine. So before I display
> any page, if the file-open fails, I want to display an error page.
>
> PROBLEM: I get to the error subroutine (I should because the DB
> parameter filename given is wrong and I tested this fact by giving some
>
> print statements and observing the command line output) but am
> automatically redirected to the main page through the
> 'display_entry_screen' subroutine (details of that sub not shown
> here)
>
> How do I make it stay on the error page?
>
> I could possibly define the 'display_error_page' as a run mode and
> use the cgiapp_prerun and the mode_prerun methods to go to it
> but this 'display_error_page' is a common method I want to call from
> my 'child' modules and I feel defining it as an additional run mode
> will prove restrictive.
>
> I guess my question could also be rephrased as:
>
> Is there a way of breaking the run-mode path of a CGI::Application
> to throw an error page?
>
> Any help is appreciated. Thanks for your patience in reading this
> long post.
>
> Sharad
>
>
>
> __________________________________
> Do you Yahoo!?
> Read only the mail you want - Yahoo! Mail SpamGuard.
> http://promotions.yahoo.com/new_mail
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

Hello Sharad and others,
I am not sure about your question except that I think it requires the use of a command like exit(0);. This always exits HTML pages and waits for a response to checkboxes etc., so I assume it would do the same thing for an error
page,
Kieran.


This message was sent through MyMail http://www.mymail.com.au


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com