- 论坛徽章:
- 0
|
#! /usr/local/bin/perl
use CGI;
a;
b;
sub a{
my $q=new CGI;
$x=$q->param('upload');
......
}
sub b{
my $q=new CGI;
$file=$q->upload('upload');
.....
}
Hi,
I would suggest you write it like below:
use strict;
use CGI ();
my $q = CGI->new;
a($q);
b($q);
sub a {
my $q=shift;
do something with $q...
}
sub b {
my $q = shift;
do something with $q...
}
Using OO method like $q->param() is better than module method which call the param() directly.When you say $q->param(),you access the param() method in CGI.pm by the way of packate to package,the param() is not in your main script's symbol space but in CGI.pm's.But when you say param() directly,you have loaded the param() subroutine into the main script's symbol space essentially,this is distinctly the memory waste.
Consider this case under mod_perl:
Such an apache child it load 3 CGI scripts:
cat a.pl
use CGI;
print param('abc');
cat b.pl
use CGI;
print param('abc');
cat c.pl
use CGI;
print param('abc');
Now this apache child load 4 param() subroutines in its process memory space (3 scripts' + CGI.pm's).
But when you write them like:
cat a.pl
use CGI ();
my $q=CGI->new;
print $q->param('abc');
cat b.pl
use CGI ();
my $q=CGI->new;
print $q->param('abc');
cat c.pl
use CGI ();
my $q=CGI->new;
print $q->param('abc');
You load only one param() subroutine in that apache's memory space,it belong to CGI.pm,and you access it via package's ref,which is called object.
Hope this helps! |
|