Subject: | PATCH: Document CGI->new, not "new CGI" |
It is considered a best practice to create objects in this syntax:
CGI->new();
rather than this:
new CGI();
Because many people use the CGI docs for example code, the attached
patch updates all the places in the CGI.pm code where this done to use
the recommended syntax.
Thanks for maintaining CGI.pm, Lincoln.
Subject: | CGI.pm.best_practice.patch |
--- /home/mark/tmp/CGI.pm.orig 2008-09-08 13:36:52.000000000 -0400
+++ /home/mark/tmp/CGI.pm.new 2008-11-21 14:45:19.000000000 -0500
@@ -2801,7 +2801,7 @@
push(@param,'-secure'=>$secure) if $secure;
push(@param,'-httponly'=>$httponly) if $httponly;
- return new CGI::Cookie(@param);
+ return CGI::Cookie->new(@param);
}
END_OF_FUNC
@@ -3475,7 +3475,7 @@
# choose a relatively unpredictable tmpfile sequence number
my $seqno = unpack("%16C*",join('',localtime,grep {defined $_} values %ENV));
for (my $cnt=10;$cnt>0;$cnt--) {
- next unless $tmpfile = new CGITempFile($seqno);
+ next unless $tmpfile = CGITempFile->new($seqno);
$tmp = $tmpfile->as_string;
last if defined($filehandle = Fh->new($filename,$tmp,$PRIVATE_TEMPFILES));
$seqno += int rand(100);
@@ -3587,7 +3587,7 @@
# choose a relatively unpredictable tmpfile sequence number
my $seqno = unpack("%16C*",join('',localtime,grep {defined $_} values %ENV));
for (my $cnt=10;$cnt>0;$cnt--) {
- next unless $tmpfile = new CGITempFile($seqno);
+ next unless $tmpfile = CGITempFile->($seqno);
$tmp = $tmpfile->as_string;
last if defined($filehandle = Fh->new($param,$tmp,$PRIVATE_TEMPFILES));
$seqno += int rand(100);
@@ -4235,7 +4235,7 @@
#!/usr/local/bin/perl -w
use CGI; # load CGI routines
- $q = new CGI; # create new CGI object
+ $q = CGI->new; # create new CGI object
print $q->header, # create the HTTP header
$q->start_html('hello world'), # start the HTML
$q->h1('hello world'), # level 1 header
@@ -4374,7 +4374,7 @@
=head2 CREATING A NEW QUERY OBJECT (OBJECT-ORIENTED STYLE):
- $query = new CGI;
+ $query = CGI->new;
This will parse the input (from both POST and GET methods) and store
it into a perl5 object called $query.
@@ -4384,7 +4384,7 @@
=head2 CREATING A NEW QUERY OBJECT FROM AN INPUT FILE
- $query = new CGI(INPUTFILE);
+ $query = CGI->new(INPUTFILE);
If you provide a file handle to the new() method, it will read
parameters from the file (or STDIN, or whatever). The file can be in
@@ -4397,7 +4397,7 @@
references to file handles, or even references to filehandle globs,
which is the "official" way to pass a filehandle:
- $query = new CGI(\*STDIN);
+ $query = CGI->new(\*STDIN);
You can also initialize the CGI object with a FileHandle or IO::File
object.
@@ -4414,29 +4414,29 @@
You can also initialize the query object from an associative array
reference:
- $query = new CGI( {'dinosaur'=>'barney',
+ $query = CGI->new( {'dinosaur'=>'barney',
'song'=>'I love you',
'friends'=>[qw/Jessica George Nancy/]}
);
or from a properly formatted, URL-escaped query string:
- $query = new CGI('dinosaur=barney&color=purple');
+ $query = CGI->new('dinosaur=barney&color=purple');
or from a previously existing CGI object (currently this clones the
parameter list, but none of the other object-specific fields, such as
autoescaping):
- $old_query = new CGI;
- $new_query = new CGI($old_query);
+ $old_query = CGI->new;
+ $new_query = CGI->new($old_query);
To create an empty query, initialize it from an empty string or hash:
- $empty_query = new CGI("");
+ $empty_query = CGI->new("");
-or-
- $empty_query = new CGI({});
+ $empty_query = CGI->new({});
=head2 FETCHING A LIST OF KEYWORDS FROM THE QUERY:
@@ -4642,7 +4642,7 @@
open (OUT,">>test.out") || die;
$records = 5;
foreach (0..$records) {
- my $q = new CGI;
+ my $q = CGI->new;
$q->param(-name=>'counter',-value=>$_);
$q->save(\*OUT);
}
@@ -4651,7 +4651,7 @@
# reopen for reading
open (IN,"test.out") || die;
while (!eof(IN)) {
- my $q = new CGI(\*IN);
+ my $q = CGI->new(\*IN);
print $q->param('counter'),"\n";
}
@@ -4832,7 +4832,7 @@
extension. This lets you go wild with new and unsupported tags:
use CGI qw(-any);
- $q=new CGI;
+ $q=CGI->new;
print $q->gradient({speed=>'fast',start=>'red',end=>'blue'});
Since using <cite>any</cite> causes any mistyped method name
@@ -5302,7 +5302,7 @@
browser. Usually these parameters are calls to functions defined in the
B<-script> field:
- $query = new CGI;
+ $query = CGI->new;
print header;
$JSCRIPT=<<END;
// Ask a silly question
@@ -5765,7 +5765,7 @@
into your fields. If you wish to turn off automatic escaping, call the
autoEscape() method with a false value immediately after creating the CGI object:
- $query = new CGI;
+ $query = CGI->new;
autoEscape(undef);
I<A Lurking Trap!> Some of the form-element generating methods return
@@ -6949,7 +6949,7 @@
form:
use CGI;
- $query = new CGI;
+ $query = CGI->new;
$riddle = $query->cookie('riddle_name');
%answers = $query->cookie('answers');
@@ -7358,7 +7358,7 @@
As a shortcut, you can interpolate the entire CGI object into a string
and it will be replaced with the a nice HTML dump shown above:
- $query=new CGI;
+ $query=CGI->new;
print "<h2>Current Values</h2> $query\n";
=head1 FETCHING ENVIRONMENT VARIABLES