[Zope-Perl] getting a list of form field names
Gisle Aas
gisle@ActiveState.com
09 Aug 2000 20:01:04 +0200
Maarten Slaets <mslaets@goldridge.net> writes:
> Using the REQUEST object in a PerlMethod I'm trying to get a list of the
> form field names, because I don't know them. I have used the keys()
> function but it seems to return a list in string format.
>
> ------------------------------------
> arguments: REQUEST
> body:
>
> my $req = shift;
> my $ret;
> my $keys;
>
> $keys = $req->keys();
What you get here is a reference to a python list object. You can use
methods like $keys->Length and $keys->GetItem($i) to extract stuff
from it.
> $keys =~ s/^\[(.*?)\]$/$1/;
But, when you use it as a string it will stringify into "[elem1,
elem2,...]". This also happen when you print it.
> foreach (split(/,/, $keys)) {
> s/^\s?'(.*?)'\s?$/$1/;
> $ret .= "$_\n";
> }
A better way would be to write:
$keys = $req->keys;
for $i (0 .. $keys->Length - 1) {
$ret .= "$_\n";
}
but with my current version (did not get into 'a2') I made methods
which return python sequences (that includes lists) that are called in
array context unwrap the list. That allow you to write the code as:
for ($req->keys) {
$ret .= "$_\n";
}
> ['field1', 'field2', ...]
> so I did some splitting
>
> Is there a way to do something like:
> @formfields = $req->keys()
This works in my version.
> or even better:
> %formdata = $req->keys()
$req->items gives you a list of (key, value) tuples. You should be
able to assign it to a hash with code like:
%formdata = map { $_->GetItem(0) => $_->GetItem(1) } $req->keys()
but it is kind of ugly. Perhaps we need to recognize certain objects
and rebless into a more specific perl class (like "Zope::Request")
that modify methods to be more perl friendly.
Regards,
Gisle