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

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

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.