http://www.perlmonks.org?node_id=556047

sara2005 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts,

First, sorry if I had asked too many questions but all seem related to me. I recently started with perl and was happily working with the CGI.pm module and some modules from CPAN to create my web app. When I had doubts, I started searching in the web and referred to CGI programming in Perl by Shishir & Scott, online docs, etc, but was not to able to answer some of the (basic!!) questions. Hence, I am here hoping to get some wisdom from the experts. Would appreciate your thoughts, comments on the following.

1) I am creating ~10 web-pages using CGI.pm. As I need common headers and footers for the page, I call a subroutine to do that, which is defined in a separate module (say, common_subs.pm). In the first few lines of each cgi script, I instruct to "use common_subs" and "use strict" but it looks like I have to create a new cgi object in the main program as well as the module (which has the common subs). Since I do that twice, would that have any impact on the performance? especially with medium duty scripts? (If I don't use "use strict",I just create a new CGI object in the main script and the code is working fine. anyway, I am going to use "use strict" in my code by default but would appreciate your reasoning)

Here is some portion of the code..

#! /path/to/perl -wT # THis is the login validation script # Import CGI module use strict; use CGI qw(:all delete_all escapeHTML); use common_subs; use "path/to/other/modules/"; # Create a new CGI object my $query = new CGI; # Create query objects to retrieve input my $username=$query->param('user'); my $passwd=$query->param('password'); my $next_page = "http://somepage.com"; my $loginOK = check_login($username,$passwd); print $query->header; print $query->start_html(-title=>'Validate Login',-bgcolor=>'white', - +style=>{src=>'/IMG_PATH/templatestyles.css'}); # This is the common header defined in common_subs and # I use that in all scripts &display_header("Check Login"); if ($loginOK == 1) { print $query->b("Login successful! Loading next page....\n"); print $query->start_form(-name=>'data',-action=>$next_page); print $query->end_form(); print $query->script("setTimeout(\"document.data.submit();\",5000);"); } else{ print $query->font({-color=>"red"},$query->p("Login Failed!!!") +, $query->br(), $query->p("Please go back to login screen and enter c +orrect userid and password!!!")); ... etc... ... } # This is the common footer defined in common_subs and # I use that in all scripts &display_footer; print $query->end_html();

and the common subroutine is defined as follows in common_subs.pm:-

use CGI qw/:standard :html3 :all/; use strict; sub check_login{ .... } sub display_header { my $disp_text=$_[0]; # Note that I have to create a new CGI object here in the sub my $query = new CGI; print $query->center; print $query->table( { -border=>0}, Tr([ td( {-align=>"center" },[ $query->img({-align=>"center", +-src=>"logo.gif ", -border=>0 }), $query->font({-size= +>'4'},$query->b( $disp_text)), ]), ]), ); } sub display_footer{ ... ... }

2) I have downloaded some modules from CPAN and installed them in a different path. I define it as "use /path/to/other/modules" at the top of each script and also set some standard environment variables. Can I define these in the common_subs.pm , which I call in all scripts? (the idea is to define common items in one file so that it would be easy for maintenance)

3) I read about the HTML::Template module after having started doing my scripts in CGI.pm. Is it fair to say that if I use HTML::Template, my scripts would be organized better than just using CGI.pm? Again, is there any impact on the performance in terms of using HTML::Template rather than CGI.pm alone?

4) I have a tendency to create separate cgi scripts for each task. i.e., I call separate scripts to check login, execute commands, read and display menus, etc.. etc.. Should I minimize the no of cgi programs (as I know that each invocation of cgi script has some overheads - I use Apache, no of max users at a given moment not to exceed 15) or it is ok to go ahead with my approach?

5) How easy (or difficult) it is to move to mod_perl or any other better technology? (there are some links I found in the search but they are fairly old !!!)

As always, I look for your valuable comments