[Zope-Perl] getting a list of form field names

Maarten Slaets mslaets@goldridge.net
Thu, 10 Aug 2000 10:21:37 +0200


Gisle Aas wrote:
> >
> > $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";
>   }

better indeed. I'm using it now (if anyone wants to use this code,
change $_ to $keys->GetItem($i))

> 
> 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";
>   }

Ah great. I'll use this when a3 is out.

> 
> > ['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.

I don't know how the Python::Object works. Is there a way you can
recognize if a python object is a dictionary or a list or ... and then
'convert' it to a perl %hash, @list or ... inside Python::Object? If you
can, then you will avoid creating a lot of Zope::Request alikes. But
again I don't know how you do your magic. :)

> 
> Regards,
> Gisle

Cheers
Maarten