For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > April 2004 > How to pass arrays and hashes in cgi forms









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to pass arrays and hashes in cgi forms
Seth E Seligman

2004-04-14, 11:31 pm

I'm starting to program in Perl after programming in PHP. Is it possible to pass an array or hash variable in a form to a cgi script? In php , if I had two form elements called field[1] and field[2] the receiving php script would convert them into an arra
y of two elements. Perl seems to just treat these as scalars called "field[1]" and "field[2]". Can arrays of text input fields be passed to Perl cgi scripts, and if so, how?
Sukhbir Dhillon

2004-04-15, 1:31 am

On Thu, 15 Apr 2004 02:10:17 +0000, Seth E Seligman wrote:

> I'm starting to program in Perl after programming in PHP. Is it possible
> to pass an array or hash variable in a form to a cgi script? In php , if
> I had two form elements called field[1] and field[2] the receiving php
> script would convert them into an array of two elements. Perl seems to
> just treat these as scalars called "field[1]" and "field[2]". Can arrays
> of text input fields be passed to Perl cgi scripts, and if so, how?


use cgi.pm module and it takes care of everything using funtion param().
Dave Cross

2004-04-15, 3:31 am

On Thu, 15 Apr 2004 02:10:17 +0000, Seth E Seligman wrote:

> I'm starting to program in Perl after programming in PHP. Is it possible
> to pass an array or hash variable in a form to a cgi script? In php , if
> I had two form elements called field[1] and field[2] the receiving php
> script would convert them into an array of two elements. Perl seems to
> just treat these as scalars called "field[1]" and "field[2]". Can arrays
> of text input fields be passed to Perl cgi scripts, and if so, how?


If you have two form fields, both called field then you can use

use CGI ':cgi';

my @fields = param('field');


Dave...

Joe Smith

2004-04-15, 4:31 am

Sukhbir Dhillon wrote:

> On Thu, 15 Apr 2004 02:10:17 +0000, Seth E Seligman wrote:
>
>
>
>
> use cgi.pm module and it takes care of everything using funtion param().


If you "use CGI;" to create the form, then it will automatically
create array references for multi-valued elements. Note where
"perldoc CGI" shows this example:
$_ = $q->param_fetch('address')->[1];
$q->param_fetch('address')->[1] = '1313 Mockingbird Lane';

On the other hand, if the HTML that comprises the FORM was created
by something other than CGI.pm, the param hash could very
well have keys that are strings with embedded square brackets.

%params = $q->Vars;
print "field:1 = $params{'field[1]'}\n";

Last time I processed a form that had a variable number of related
fields, I used something like this:

my %params = $q->Vars;
my %data;
while (my($key,$val) = each %params) {
if ($key =~ /(.*)\[(\d+)\]$/) {
$data{$1}[$2] = $val; # Array with plain index
} elsif ($key =~ /^(\w+)(.*)/ {
$data{$1}{$2} = $val; # Hash or multi-dim array
} else {
$data{OTHER}{$key} = $val; # None of the above
}
}

Data can then be copied to simple arrays or hashes for convenience.

my @host_ip = @{$data{host_ip}};
my @host_name = @{$data{host_name}};
warn "Host table mismatch" unless @host_ip == @host_name;
my @alias_name = @{$data{alias_name}};
my @alias_cname = @{$data{alias_cname}};
warn "Alias table mismatch" unless @alias_name == @alias_cname;
my %mx_host = @{$data{mx_host}};

-Joe
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com