Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Converting common subroutines as a module by sara2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-25 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found