--- "G.P.Gaudreault" <[EMAIL PROTECTED]> wrote:
> All,
> 
> Which is faster, using the CGI.pm param function to get form input, or parsing out 
>the hash
> within my script?  I've got a script that currently uses a lot of system resources, 
>and I'm
> looking for ways to optimize it.

[snip]

> Is it faster to run this, or use CGI.pm and call param()?
> 
> -GPG

Your routine is faster.  It's also chock full o'bugs.  For a good discussion as to 
why, check out
http://www.easystreet.com/~ovid/cgi_course/lesson_two/lesson_two.html.

Ironically, earlier today I asked someone to parse their form-handling code stating 
that I would
find at least 5 bugs.  The person replied that s?he hadn't really written a 
form-handling routine
but a form *validating* routine.  Silly me, not seeing the context.  Now, you come 
along with your
form-handling routine and I just happen to have my gun loaded, so please don't take 
any of the
following personally :)

Your routine has a variety of issues, including some novel ones that I haven't 
stumbled across
before.  All comments will refer to the line of code following them.  I am not trying 
to be cruel,
this is just another example of why "rolling your own" is usually a bad idea.

######################################################################
sub getform 
    # Looks like we're not using strict!  Need to declare variables with 'my'.
    $buffer = "";

    # Doesn't allow for GET requests (or HEAD or a variety of others, but we rarely
    #     encounter those in CGI scripts.
    # There is no test to see if the read() is successful
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

    # Doesn't allow for an alternate delimiter.  The semicolon (;) is supposed to be 
the
    # new delimiter and systems are slowly moving towards that.  Those will break with 
this
    # code.
    @pairs=split(/&/,$buffer);

    foreach $pair (@pairs) {
        @a = split(/=/,$pair);
        $name=$a[0];
        $value=$a[1];

        # why didn't we do this with $name?
        $value =~ s/\+/ /g;

        # why didn't we do this with $name?
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        # why are we throwing away data that we might need in the future?
        # what if you need data from a textarea tag?
        $value =~ s/[\r\n]//g;

        # why not add them to the %form hash?
        push (@data,$name);
        push (@data,$value);
    }
    # Oops!  We just lost all multiple values.
    %form=@data;

    # returning a reference to the hash instead of the hash is probably faster, 
    # especially if you're working with a large hash.
    %form;
}
######################################################################

Let us know if you have any more questions.

Cheers,
Curtis Poe

=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Reply via email to