[Zope-Perl] Python void context

Gisle Aas gisle@ActiveState.com
27 May 2000 06:40:48 +0200


Ken Manheimer <klm@digicool.com> writes:

> Gisle Aas gisle@ActiveState.com writes:
> > Chris McDonough <chrism@digicool.com> writes:
> > 
> > > It occurs to me that you could define a context default keyword argument
> > > to the Perl function e.g.
> > > 
> > > obj.func(1,2,context="list")
> > > obj.func(1,2,context="scalar")
> > > obj.func(1,2,context="void")
> > > 
> > > Where func's signature defines context as a default:
> > > 
> > > func(self, arg1, arg2, context="list")
> > > 
> > > This seems to be the 'pythonic' way of dealing with such a thing.  I'm
> > > not sure how you're implementing a Perl function wrapper, so I'm not
> > > sure if this suits you.
> > 
> > Currently I just ignore keyword arguments so this is doable.  It might
> > be in conflict with other mappings of keyword arguments that we want
> > to do insted, but reserving some namesspace like __*, i.e. use
> > __context="list" etc. should be ok.
> 
> I think this is a good idea!  In particular, to use something with a
> leading *and* trailing '__':
> 
>   obj.method(args, __context__="scalar")
> 
> (maybe also spelled obj.method(args, __context__="$")

Ok. I have now implemented this as __wantarray__ since that maps
directly to the user visible perl builtin.  I also made the
__wantarray__ attribute of perl reference objects accessible.  The
keyword argument will override the attribute.

Void context is to be signalled with __wantarray__ = None, but that is
not implemented yet.

$ 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
>>> t = perl.eval("sub { localtime }")
>>> t   
<perl ref object at 81fbfb8>
>>> t.__wantarray__
0
>>> t()
'Sat May 27 06:35:50 2000'
>>> t(__wantarray__ = 1)
(10, 37, 6, 27, 4, 100, 6, 147, 1)
>>> t.__wantarray__ = 1
>>> t()
(6, 36, 6, 27, 4, 100, 6, 147, 1)
>>> t(__wantarray__ = 0)
'Sat May 27 06:36:24 2000'