[Zope-Perl] Perl Internal Methods -- accessing PARENTS?

Gisle Aas gisle@ActiveState.com
08 Aug 2000 20:20:35 +0200


Joseph Wayne Norton <norton@alum.mit.edu> writes:

> Using the Perl Internal Method, I'm trying to access the "PARENTS"
> object and to allow me access to the properties of parent folders. I'm
> having trouble with the GetItem method and not sure how to access the
> properties of the parent objects.
> 
> 
> Here is what I have done so far:
> 
> 1. I made the following modification to methodTry.dtml to define all
> Zope-defined Web request variables.
> 
> # diff methodTry.dtml methodTry.dtml.orig  
> 22c22 
> <         <dtml-if "_['sequence-item'] in ('self', 'REQUEST', 'RESPONSE', 'AUTHENTICATED_USER', 'AUTHENTICATION_PATH', 'PARENTS', 'URL')"> 
> --- 
> >         <dtml-if "_['sequence-item'] in ('self', 'REQUEST', 'RESPONSE')"> 

Thanks! I'll patch the next version to be like this.

> 2. I created the following internal perl method:
> 
> Name: foo
> Arguments: REQUEST, PARENTS
> Code:
> 
> my $req = shift;
> my $par = shift;
> my $ret;
> 
> foreach ($req->keys) {
>  $ret .= "<p>" . $_ . "</p><br>\n";
> }
> 
> for (my $i = 0; $i < $par->Length; $i++) {
>  $ret .= "<p>" . $par->GetItem(0 + $i) . "</p><br>\n";
> }
>  
> $ret;
>  
> 
> 3. I'm facing the following difficulties
> 
>   a. I can't seem to use the GetItem interface when passing either
>     ($i) or (0 + $i). The following error is given:
> 
>  # The following does not work.
>  # Error Type: TypeError
>  # Error Value: sequence index must be integer

Perl has a tendency to prefer floats in its calculations, and python
does not like indexing by floats.  The {Get,Set,Del}Item() methods
should probably force the index to be a integer before passing it to
python.

A workaround for now can be to force python to see integers with
int(), i.e. write:

   $par->GetItem(int($i))

>   b. I hard-coded the GetItem to return item #1($par->GetItem(1)) but
>   once I have a handle on the parent, I am not sure how to access the
>   properties, etc. of the parent objects.

The specific parent is likely to be some Zope folder object so you
treat it like "self".  We can for instance obtain the "id" of the
folder by calling the "id" method:

  my $folder = $par->GetItem(int($i));
  $ret .= $folder->id;

You just need to know the name an signature of the method you want to
call.  Then you just use it directly.

> Do you have any pointers?

To the folder API documentation? Not really.  Isn't there some way to
get a list of methods supported with the Zope online help system?

Regards,
Gisle