[Zope-Perl] Need more Zope method examples

Gisle Aas gisle@ActiveState.com
9 Aug 2000 13:54:57 -0000


In order to document&verify that it is possible and convenient to
write Zope methods in Perl I have started to write the following
document.  I ask for contributions in the form of interesting (perhaps
tricky) DTML or Python methods that I can try to reimplement in perl.
If you have anything, please send it to me.

Currently I only have one example :-(

Regards,
Gisle


-----------------------------------------------------------------------
=head1 NAME

zope-method - Writing Zope methods in various languages

=head1 DESCRIPTION

The purpose of this document is to give examples of more-or-less
interesting Zope methods and show how the same method would look if it
was implemented in DTML, Python or Perl.  The main purpose is to
document the mapping between languages, so that when you read
documentation that use some specific language for its examples, you
can easily map it to your preferred implementation language.

These examples should also serve as background information for
discussions on improving the Zope Perl support, so that perl methods
can be written without too much knowledge about the fact that Zope is
implemented in Python underneath.

=head1 EXAMPLE 1

In the first example we have a method that list the content of the
folder "f1".  The DTML method code looks like this:

   <dtml-in "f1.objectValues()">
   <dtml-var id>: <dtml-var sequence-item>
   </dtml-in>
   done

The same as a Python method would look like this:

   Param: self

   for item in self["f1"].objectValues():
       print item.id(), ": ", item
   print "done"
   return printed

The same as a Perl method would look like this:

   Param: self

   my $self = shift;
   my @res;

   for ($self->GetItem("f1")->objectValues) {
       push(@res, join(": ", $_->id, $_));
   }
   join("\n", @res, "done");

Alternative Perl method that does not use the easy way to access
attributes or calling methods, but always use full API calls.

   Param: self

   my $self = shift;
   my @res;

   my $list = $self->GetItem("f1")->GetAttr("objectValues")->Call();

   for my $i (0 .. $list->Length - 1) {
       my $item = $list->GetItem($i);
       push(@res, join(": ", $item->GetAttr("id")->Call(), $item->Str));
   }
   join("\n", @res, "done");


=head2 Discussion

The <dtml-in> construct is the same as a for-loop in both python and
perl.  Otherwise the code should be easy enough to read.

In perl we currently doesn't have a short form for indexing objects,
so we have to resort to the GetItem() method.  It would make sense to
be able to say $self->["f1"] (or $self->{"f1"}), but there are some
techical problems with that for now.

You will also see that in perl we need to both specify the arguments
and also shift them out out @_.  For perl internal methods the first
shift statement should probably be autogenerated, so we don't have to
write it ourself.  For instance if the arguments are "C<self,
RESPONSE, foo>", then a statement like this should automatically be
prepended to the method:

  my($self, $RESPONSE, $foo) = @_;

For perl external methods we can't do that, so here you would have to
specify your arguments twice.

For python methods there is some magic going on with printing and the
variable C<printed>.  This can be replicated for perl with the use of
tied file handles.  I am not sure it is a good idea yet.  Is it
agreement that this is a good idea for PythonMethods?

...