Date: Fri, 29 Jun 2001 10:42:06 -0700 From: "Godzilla!" Subject: Re: Textarea to file Message-Id: <3B3CBDEE.470E901@stomp.stomp.tokyo> TuNNe|ing wrote: > This is some EDITED sample code I got from Perl 5 by Example by David > Medinets. (snipped code) Using very similar Perl 4 style coding, there is a method which will run not quite twice as fast as your edited textbook Perl 5 example and, my method will correctly remove form action generated carriage return / newline \r\n combinations where your textbook code does not. My code also provides direct access to key / value pairs where your textbook code requires hash access to key / value pairs. I have included conversion to
\n within my code to maintain equality in controls and to provide a superior document source print along with an if / else for GET or POST method. Normally I use only one or the other tailored to a specific form action request method. name1 is my method name2 is your textbook method Godzilla! -- #!perl print "Content-type: text/plain\n\n"; use Benchmark; print "Run One:\n\n"; &Time; print "\n\nRun Two:\n\n"; &Time; print "\n\nRun Three:\n\n"; &Time; sub Time { timethese (100000, { 'name1' => '$ENV{QUERY_STRING} = "Text_Area=Godzilla Rocks!"; &Akostininchi_Ithana; sub Akostininchi_Ithana { if ($ENV{REQUEST_METHOD} eq "GET") { $in = $ENV{QUERY_STRING}; } else { read (STDIN, $in, $ENV{CONTENT_LENGTH}); } local (*in) = @_ if @_; local ($i, $key, $value); @in = split (/&/, $in); foreach $i (0 .. $#in) { $in[$i] =~ s/\+/ /g; ($key, $value) = split (/=/, $in[$i], 2); ($value eq "") && next; $key =~ s/%(..)/pack ("c",hex($1))/ge; $value =~ s/%(..)/pack ("c",hex($1))/ge; $value =~ s/\r\n/
\n/g; $in{$key} .= "\0" if (defined($in{$key})); $in{$key} .= $value; return 1; } }', 'name2' => '$ENV{QUERY_STRING} = "Text_Area=Godzilla Rocks!"; &getFormData; sub getFormData { my($hashRef) = shift; my($buffer) = ""; if ($ENV{REQUEST_METHOD} eq "GET") { $buffer = $ENV{QUERY_STRING}; } else { read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); } foreach (split(/&/, $buffer)) { my($key, $value) = split(/=/, $_); $key = decodeURL($key); $value = decodeURL($value); $value =~ s!\n!
!g; %{$hashRef}->{$key} = $value; } } sub decodeURL { $_ = shift; tr/+/ /; s/%(..)/pack("c", hex($1))/eg; return($_); }', } ); } exit; PRINTED RESULTS: ________________ Run One: Benchmark: timing 100000 iterations of name1, name2... name1: 5 wallclock secs ( 4.78 usr + 0.00 sys = 4.78 CPU) @ 20920.50/s name2: 9 wallclock secs ( 8.07 usr + 0.00 sys = 8.07 CPU) @ 12391.57/s Run Two: Benchmark: timing 100000 iterations of name1, name2... name1: 5 wallclock secs ( 4.83 usr + 0.00 sys = 4.83 CPU) @ 20703.93/s name2: 8 wallclock secs ( 8.08 usr + 0.00 sys = 8.08 CPU) @ 12376.24/s Run Three: Benchmark: timing 100000 iterations of name1, name2... name1: 5 wallclock secs ( 4.89 usr + 0.00 sys = 4.89 CPU) @ 20449.90/s name2: 8 wallclock secs ( 8.18 usr + 0.00 sys = 8.18 CPU) @ 12224.94/s