develooper Front page | perl.beginners | Postings from January 2002

Re: What's wrong with this?

Thread Previous
From:
Jan Gruber
Date:
January 31, 2002 03:11
Subject:
Re: What's wrong with this?
Message ID:
200201311108.g0VB8Zu09740@pollux.intern.primacom.net
On Thursday 31 January 2002 11:45 am, you wrote:
> > >> If you want to loop over all the form fields, you'd do:
> > >>
> > >>   for $field (param()) {
> > >>     print "$field => ", param($field), "<br>\n";
> > >>   }
> > >
> > >How can the param's be placed into a new hash?
> >
> > CGI.pm has a Vars() method, I believe, which returns a hash.
> >
> >   use CGI;
> >   my $q = CGI->new;
> >   $data = $q->Vars;
> >
> >   print $data->{field};  # etc.
> >
> > But note that this can cause annoyances when you have multiple fields of
> > the same name (like checkboxes).  The values will be \0-separated.
>
> Fields are all unique.  I read the Vars section of 'perldoc CGI', but
> didn't figure out how to use it.  Wound up with below, maybe some sort of
> better way, not sure.  Reasons for wanting to do this:
>
> 1.  Rumored CGI has solid security.
> 2.  Looks a lot cleaner.
> 3.  Script contains $data{'something'} = "foo"; # redefinitions I don't
> think can be done with param('something') = "foo"; (primary reason).
>
> The problem with this is that it is slower than the old routine due to the
> 'for' loop.
>
> sub get_data {
>
> 	$t0 = new Benchmark;
>
> #   local($string);
> #   if ($ENV{'REQUEST_METHOD'} eq 'GET') {
> #      $_ = $string = $ENV{'QUERY_STRING'};
> #      tr/\"~;/_/;
> #      $string = $_;
> #   } else {
> #      read(STDIN, $string, $ENV{'CONTENT_LENGTH'});
> #      $_ = $string;
> #      $OK_CHARS='a-zA-Z0-9=&%\n\/_\-\.@';
> #      tr/\"~;/_/;
> #      $string = $_;
> #   }
> #   @data = split(/&/, $string);
> #   foreach (@data)
> #   {
> #      split(/=/, $_);
> #      $_[0] =~ s/\+/ /g;
> #      $_[0] =~ s/%(..)/pack("c", hex($1))/ge;
> #      $data{"$_[0]"} = $_[1];
> #   }
> #   foreach (keys %data)
> #   {
> #      $data{"$_"} =~ s/\+/ /g;
> #      $data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge;
> #   }
>
> 	use CGI 'param'; 		# These lines replace those above.
> 	for (param()) {
> 	        $data{$_} = param($_);
> 	}
>
> 	$t1 = new Benchmark;
> 	$td = timediff($t1, $t0);
> 	print "Code took:  ", timestr($td), "\n\n";
>
>     %data;
>
> 	# Benchmark results:
> 	# Old routine:  Code took: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01
> CPU) # CGI:          Code took: 0 wallclock secs ( 0.08 usr + 0.00 sys =
> 0.08 CPU) }
>
> /g

-- 
Jan Gruber              Primacom AG
Central Systems

Office: +49 (341) 609 524 53
Fax:	+49 (341) 609 525 17

cat /dev/world | perl -e "while (<>) {(/(^.*? \?) 42 \!/) && (print $1)}"
errors->(c)
_ 

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About