[Zope-Perl] No need to declare perl method parameters twice any more

Gisle Aas gisle@ActiveState.com
09 Aug 2000 20:57:29 +0200


Gisle Aas <gisle@ActiveState.com> writes:

> The same as a Perl method would look like this:
> 
>    Param: self
> 
>    my $self = shift;

[...]

> 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) = @_;

This was such a good idea that I also provide an implementation (patch
relative to a2).

Regards,
Gisle


Index: PerlMethod/__init__.py
===================================================================
RCS file: /home/cvs/activestate/bifrost/PerlMethod/__init__.py,v
retrieving revision 1.4
diff -u -p -u -r1.4 __init__.py
--- PerlMethod/__init__.py	2000/08/03 12:41:43	1.4
+++ PerlMethod/__init__.py	2000/08/09 18:51:09
@@ -23,11 +23,13 @@ def get_compile():
         compile = perl.eval("""
             use strict;
             sub {
+                my $args = shift;
                 my $code = shift;
-                my $name = shift || "eval";
+                my $name = shift || "perl-method";
                 my @warnings;
                 local $SIG{__WARN__} = sub { push(@warnings, @_); };
-                $code = qq(sub {\n# line 1 "$name"\n$code \n});
+                $args = "my($args) = \@_;\n" if $args;
+                $code = qq(sub {\n$args# line 1 "$name"\n$code \n});
                 my $f = eval $code;
                 if ($@) {
                     my $err = join("", $@, @warnings);
@@ -51,6 +53,11 @@ class FuncCode:
     def args(self):
         return join(self.co_varnames, ", ")
 
+    def perl_args(self):
+        perl_names = []
+        for n in self.co_varnames:
+            perl_names.append("$" + n)
+        return join(perl_names, ", ")
 
 # This is the actual class that we will store instances of, in the
 # ZODB as this product is instantiated through manage_addPerlExtMethod
@@ -105,7 +112,7 @@ class PerlMethod(OFS.SimpleItem.Item, Pe
         # try to trigger early error if something is bad with
         # the code.
         compile = get_compile()
-        self._v_f = compile(code, self.id)
+        self._v_f = compile(self.func_code.perl_args(), code, self.id)
         
         if REQUEST: return MessageDialog(
             title  ='Changed %s' % self.id,
@@ -121,7 +128,7 @@ class PerlMethod(OFS.SimpleItem.Item, Pe
         # The "_v_" prefix tells ZODB not try to store this thing
         if not hasattr(self, "_v_f"):
             compile = get_compile()
-            self._v_f = compile(self.code, self.id)
+            self._v_f = compile(self.func_code.perl_args(), self.code, self.id)
 
         return apply(self._v_f, args, kw)