Hello Monks,
I am a little confused as to how to handle common subroutines that are used by multiple CGI scripts. Looking at this node , I understand that there are different ways to handle this.
I have basically followed the last suggested approach (i.e. having all the common routines in a separate module called as 'common.pm' and then call it in the scripts I need.)
Here is the skeleton for the common.pm module...
package common;
use strict;
use Exporter;
use CGI qw/:standard :delete_all :escapeHTML :html3 :all/;
# Setup to export common subroutines and variables used by many script
+s
our ( @ISA, @EXPORT_OK, $q );
@ISA = qw( Exporter );
@EXPORT_OK = qw( $query
is_tainted
check_user
authenticate
set_path
print_header
print_footer
error_form
log_msg
);
# Instantiate a new CGI object
$query = CGI->new;
sub check_user {
....
}
sub is_tainted {
....
}
sub authenticate {
....
}
sub set_path {
....
}
sub print_header {
....
}
sub print_footer {
....
}
sub error_form {
....
}
sub log_msg {
....
}
1;
and here is the sample script in which I use the module
use strict;
use lib "/path/to/lib";
use common qw( $query
is_tainted
check_user
authenticate
set_path
print_header
print_footer
error_form
log_msg
);
# Create query objects to retrieve input
my $username = $query->param( "user" );
# Display error message if the query objects are not found
unless ( defined $username ) {
error_form( "<msg to display>" );
exit;
}
# Set path
my $dir_path = set_path( $username );
# Check if $dir_path is set correctly
if ( $dir_path eq "Error" ) {
error_form( "Error setting path" );
exit;
}
# Authenticate
my $valid_code = authenticate( $username);
if ( $valid_code eq "FALSE" ) {
log_msg( <messages> );
error_form( "Invalid code" );
exit;
}
#print the html page
print $query->header();
print $query->start_html(-title => 'Welcome',
-bgcolor => 'white',
);
print_header( "Welcome" );
print $query->start_form( -name => 'data', -action => 'page2' );
print $query->hidden( -name => "user", -value => $username );
...
etc..
...
print $query->end_form();
print $query->table( { -border=>0},
$query->Tr( [ $query->td( [ $query->a( {-href=>"j
+avascript:submitform()"},
$query->img({-align=>"center", -sr
+c=>"submit.gif ", -border=>0 })) ]),
] ),
);
print_footer();
print $query->end_html();
log_msg( <arguments> );
First, Is this a fairly good approach? or is it better to create individual perl scripts for each subroutine (similar to 'C')
Second, as you can see in the sample script, the code until '#print the html page' is more likely to be common across many cgi scripts. Can I make that portion as another subroutine and use it in all the scripts?
Please share your comments.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|