[Zope-Perl] Send me your Perl Methods

Gisle Aas gisle@ActiveState.com
08 Sep 2000 12:35:41 +0200


Michel Pelletier <michel@digicool.com> writes:

> Gisle Aas wrote:
> > 
> > 
> > I would actually like to see some methods that interact more with the
> > Zope environment.
> 
> Hmm.  I agree, can Zope's DOM interface be called effectivly from Perl?

I don't know what Zope's DOM interface is, but if python can call it I
am pretty sure Perl can too.

> Maybe we could create a cool perl method that worked with the DOM to
> display a simple Zope tree, something like:
> 
> foo = []
> for child in self.getChildNodes():
>   if child.getNodeName() == 'Folder':
>     foo.append(child.getNodeName())
> return foo

This can be translated to this PerlMethod:

   Arguments: self
   Code:      my @foo;
              for my $child ($self->getChildNodes) {
	          my $name = $child->getNodeName;
	          push(@foo, $name) if $name eq "Folder";
              }
              return \@foo;

But this returns a Perl array reference back to Zope.  Perl arrays
implement the python sequence protocol so in most cases that should be
ok.  You might get some trouble if you wanted that return value to be
saved in the ZODB or used in places that only accepts lists.  You can
force it to be converted to a Python list by replacing the last
statement with:

              return Python::list(@foo);

or you could start out with a Python list in the first place:

   Code:      my $foo = Python::list();
              for my $child ($self->getChildNodes) {
	          my $name = $child->getNodeName;
                  $foo->append($name) if $name eq "Folder";
              }
              return $foo;

Anyway, did you actually want to fill the list with the strings
"Folder"?  It might make more sense to fill it with $child references.

Regards,
Gisle