[Zope-Perl] Mapping of perl's list/scalar context

Gisle Aas gisle@ActiveState.com
26 May 2000 13:05:07 +0200


Perl functions can be called in either list or scalar context, and
might return very different result depending of context.  Python does
not have a similar concept, so this is what I have done to map it:

   perl.call("func")

will call the perl function &func in scalar context, and

   perl.call_tuple("func")

will call the perl function &func in list context and will always
return a tuple.

If a python variable "obj" holds a reference to a perl object then:

   obj.foo()

will call the 'foo' method in scalar context, and

   obj.foo_tuple()

will call the 'foo' method in list context and return a tuple.  There
is currently no way to call a method that is actually called
'foo_tuple' in scalar context.  Bad :-(

If a python variable "func" holds a reference to a perl function then:

   func()

will call the function in scalar context.  There is currently no way
to get it called in array context.

One idea is to give access to a __wantarray__ attribute for perl
function objects.  Then we could do it like this:

   func.__wantarray__ = 1
   func()  # will call function in array context

Other ideas or comments?



Demo:

$ python
Python 1.6a2 (#5, May 24 2000, 22:38:33)  [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Copyright 1995-2000 Corporation for National Research Initiatives (CNRI)
>>> import perl
>>> perl.eval("""                  
... package Foo;
... sub new { bless {}, shift }
... sub t { localtime }
... """)
>>> perl.call("Foo::t")
'Fri May 26 12:55:56 2000'
>>> perl.call_tuple("Foo::t")
(4, 56, 12, 26, 4, 100, 5, 146, 1)
>>> obj = perl.callm("new", "Foo")
>>> obj.t()
'Fri May 26 12:56:32 2000'
>>> obj.t_tuple()
(38, 56, 12, 26, 4, 100, 5, 146, 1)
>>> obj.t
<perl ref object at 81c4e28>
>>> func = perl.eval("\&Foo::t")
>>> func()
'Fri May 26 12:57:15 2000'
>>> func
<perl ref object at 81ff720>
>>>