PDA

View Full Version : Which is better?


sujith
07-22-2006, 10:30 AM
Methode:A
..........................................
my $query = new CGI;
$name=$query->param('name');

........................................

Methode:B
........................................
require "cgi-lib.pl";
&ReadParse(*input);
$name=$input{'name'};
........................................



The above 2 mehodes Methode:A and Methode:B
do the same ? Then what is the difference between the two?
Which is better?

Sujith

FishMonger
07-22-2006, 04:43 PM
Method B has been depreciated for about 10 years and is considered to be a poor choice in todays scripts. Method A uses better quality code and applies more checks against the input.

One nice thing about method B is that it puts all of the params into a hash, but so can method A and it does does it better.

my $query = new CGI;
my %input = $query->Vars;

Another advantage to using the CGI module as apposed to the cgi-lib is that it has methods for generating any/all html code, not just parse the input.

KevinADC
07-22-2006, 07:31 PM
agree with FishMonger. Method one is better. Method two using the very old cgi-lib.pl module was written for perl4 and should not be used except for supporting legacy scripts that are not worth trying to recode with newer modules. And even then, CGI has support for legacy scripts and you can use the old ReadParse() function directly from the CGI module.

FishMonger
07-22-2006, 08:18 PM
Here is the relevant code in the CGI module that relates to the Vars and ReadParse subroutines.
# These are some tie() interfaces for compatibility
# with Steve Brenner's cgi-lib.pl routines
'Vars' => <<'END_OF_FUNC',
sub Vars {
my $q = shift;
my %in;
tie(%in,CGI,$q);
return %in if wantarray;
return \%in;
}
END_OF_FUNC

# These are some tie() interfaces for compatibility
# with Steve Brenner's cgi-lib.pl routines
'ReadParse' => <<'END_OF_FUNC',
sub ReadParse {
local(*in);
if (@_) {
*in = $_[0];
} else {
my $pkg = caller();
*in=*{"${pkg}::in"};
}
tie(%in,CGI);
return scalar(keys %in);
}
END_OF_FUNC

COMPATIBILITY WITH CGI-LIB.PL

To make it easier to port existing programs that use cgi-lib.pl the
compatibility routine "ReadParse" is provided. Porting is simple:

OLD VERSION
require "cgi-lib.pl";
&ReadParse;
print "The value of the antique is $in{antique}.\n";

NEW VERSION
use CGI;
CGI::ReadParse();
print "The value of the antique is $in{antique}.\n";

CGI.pm's ReadParse() routine creates a tied variable named %in,
which can be accessed to obtain the query variables. Like
ReadParse, you can also provide your own variable. Infrequently
used features of ReadParse, such as the creation of @in and $in
variables, are not supported.

Once you use ReadParse, you can retrieve the query object itself
this way:

$q = $in{CGI};
print textfield(-name=>'wow',
-value=>'does this really work?');

This allows you to start using the more interesting features
of CGI.pm without rewriting your old scripts from scratch.