You are not logged in Log in Join
You are here: Home » Members » andym » wiki » ScriptperlPod

Log in
Name

Password

 
 

History for ScriptperlPod

??changed:
-
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#name">NAME</A></LI>
	<LI><A HREF="#description">DESCRIPTION</A></LI>
	<LI><A HREF="#example 1">EXAMPLE 1</A></LI>
	<UL>

		<LI><A HREF="#discussion">Discussion</A></LI>
	</UL>

	<LI><A HREF="#example 2">EXAMPLE 2</A></LI>
	<UL>

		<LI><A HREF="#discussion">Discussion</A></LI>
	</UL>

</UL>
<!-- INDEX END -->

<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>zope-method - Writing Zope methods in various languages</P>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>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.</P>
<P>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.</P>
<P>
<HR>
<H1><A NAME="example 1">EXAMPLE 1</A></H1>
<P>In the first example we have a method that list the content of the
folder ``f1''.  The DTML method code looks like this:</P>
<PRE>
   &lt;dtml-in &quot;f1.objectValues()&quot;&gt;
   &lt;dtml-var id&gt;: &lt;dtml-var sequence-item&gt;
   &lt;/dtml-in&gt;
   done</PRE>
<P>The same as a Python method would look like this:</P>
<PRE>
   Param: self</PRE>
<PRE>
   for item in self[&quot;f1&quot;].objectValues():
       print item.id(), &quot;: &quot;, item
   print &quot;done&quot;
   return printed</PRE>
<P>The same as a Perl method would look like this:</P>
<PRE>
   Param: self</PRE>
<PRE>
   my @res;
   for ($self-&gt;{f1}-&gt;objectValues) {
       push(@res, join(&quot;: &quot;, $_-&gt;id, $_));
   }
   join(&quot;\n&quot;, @res, &quot;done&quot;);</PRE>
<P>Alternative Perl method that does not use the easy way to access
attributes or calling methods, but always use full API calls.</P>
<PRE>
   Param: self</PRE>
<PRE>
   use Python qw(getitem getattr funcall len str);</PRE>
<PRE>
   my @res;
   my $list = funcall(getattr(getitem($self, &quot;f1&quot;), &quot;objectValues&quot;));</PRE>
<PRE>
   for my $i (0 .. len($list) - 1) {
       my $item = getitem($i);
       push(@res, join(&quot;: &quot;, funcall(getattr($item, id&quot;)),
                             str($item)));
   }
   join(&quot;\n&quot;, @res, &quot;done&quot;);</PRE>
<P>
<H2><A NAME="discussion">Discussion</A></H2>
<P>The &lt;dtml-in&gt; construct is the same as a for-loop in both python and
perl.  Otherwise the code should be easy enough to read.</P>
<P>For python methods there is some magic going on with printing and the
variable <CODE>printed</CODE>.  This can be replicated for perl with the use of
tied file handles.  I am not sure it is a good idea yet.</P>
<P>
<HR>
<H1><A NAME="example 2">EXAMPLE 2</A></H1>
<P>The ``Elvis Lives'' tutorial lesson 8 has an example of scripts managing
image uploads from a form that looks like this:</P>
<PRE>
   &lt;h2&gt;&lt;dtml-var title&gt;&lt;/h2&gt;</PRE>
<PRE>
   &lt;p&gt;Upload a picture to the Elvis Photo Archive.&lt;/p&gt;</PRE>
<PRE>
   &lt;form action=&quot;photoAction&quot; method=&quot;post&quot;
   enctype=&quot;multipart/form-data&quot;&gt;
   &lt;p&gt;Title: &lt;input type=&quot;text&quot; name=&quot;photo_title&quot;&gt;&lt;/p&gt;
   &lt;p&gt;File: &lt;input type=&quot;file&quot; name=&quot;file&quot;&gt;&lt;/p&gt;
   &lt;input type=&quot;submit&quot;&gt;
   &lt;/form&gt;</PRE>
<PRE>
   &lt;dtml-var standard_html_footer&gt;</PRE>
<P>The DTML method to handle upload submissions look like this:</P>
<PRE>
   &lt;dtml-var standard_html_header&gt;
   &lt;h2&gt;&lt;dtml-var title&gt;&lt;/h2&gt;</PRE>
<PRE>
   &lt;dtml-call
        expr=&quot;photoArchive.manage_addImage(
                   id='', file=file, title=photo_title)&quot;&gt;</PRE>
<PRE>
   &lt;p&gt;Thanks for your photo submission.&lt;/p&gt;
   &lt;dtml-var standard_html_footer&gt;</PRE>
<P>The same as a python method could look like this:</P>
<PRE>
   Param: self, file, photo_title</PRE>
<PRE>
   print self.standard_html_header(self)
   print &quot;&lt;h2&gt;&quot; + self.title + &quot;&lt;/h2&gt;&quot;</PRE>
<PRE>
   self[&quot;photoArchive&quot;].manage_addImage(
            id='', file=file, title=photo_title)</PRE>
<PRE>
   print &quot;&lt;p&gt;Thanks for your photo submission.&lt;/p&gt;&quot;
   print self.standard_html_footer(self)</PRE>
<PRE>
   return printed</PRE>
<P>The same as a perl method would look like this:</P>
<PRE>
   my @res;
   push(@res, $self-&gt;standard_html_header($self));</PRE>
<PRE>
   push(@res, &quot;&lt;h2&gt;&quot;, $self-&gt;title, &quot;&lt;/h2&gt;&quot;);</PRE>
<PRE>
   $self-&gt;{f1}-&gt;manage_addImage(*id    =&gt; &quot;&quot;,
                                *file  =&gt; $file,
                                *title =&gt; $photo_title);</PRE>
<PRE>
   push(@res, &quot;&lt;p&gt;Thanks for your photo submission.&lt;/p&gt;&quot;);</PRE>
<PRE>
   push(@res, $self-&gt;standard_html_footer($self));
   join(&quot;&quot;, @res);</PRE>
<P>
<H2><A NAME="discussion">Discussion</A></H2>
<P>When we invoke other DTML methods from python or perl (eg
<CODE>standard_html_header</CODE> above) then we need to pass the client object
for lookups as a separate argument.</P>
<P>The perl method is almost identical to the python method.  The only
strange thing going on is how we pass in keyword parameters.  In this
case we have used the glob trick (see <A HREF="#"></A>Python::Object).</P>