in reply to Re: Re: Web form security
in thread Web form security
Use strict and -w, except of course for one-liners and/or short throwaway scripts. Note that the warnings pragma only works under Perl v5.6.0+.use strict; use warnings; use CGI;
This instantiates the CGI object and fills @names with the parameter list, then declares %param, to be used later.my $q = CGI->new; my @names = $q->param; my %param;
Iterating over each parameter in order,foreach my $name (@names)
Set the value in the parameter hash ($param{$name}) to the parameter value ($q->param($name))...{ $param{$name} = $q->param($name);
... and print it; the $name and $param{$name} values are interpolated into the string. $q->br just generates an empty BR tag.print "$name: $param{$name}", $q->br; }
The other version:
Same as before.use strict; use warnings; use CGI; my $q = CGI->new;
This simultaneously instantiates %params and fills it with, for each element in $q->param (the parameter list), the name ($_, the placeholder variable -- see map / perlvar) plus the value ($q->param($_)). When this is put into a hash these pairs turn into keys and values.my %params = map { $_, $q->param($_) } ($q->param);
This first takes the parameter list ($q->param) and maps each element to the string "$_: $params{$_}" which is the name ($_) plus a colon, space, and value ($params{$_}) -- accessing an element of the params hash with key being the name. Then it joins these strings together with the empty BR tag, and prints the result. Hope this helps.print join $q->br, map { "$_: $params{$_}" } ($q->param);
|
|---|