[Zope-Perl] Calling a perl method called del()

Gisle Aas gisle@ActiveState.com
26 Oct 2001 07:57:50 -0700


Alexandre Gattiker <gattiker@isb-sib.ch> writes:

> I have tried pyperl as a way to use existing perl modules in python
> programs. Unfortunately, a method in my perl class is called del(),
> which is a python reserved word. This is reproduced in the example
> below. Replacing del() with another name works. Is there a way to
> circumvent the problems without changing the perl module ?

Yes.  See below.

> bug.pm :
> 
> package bug;
> 
> sub new {
> 	my $ref = shift;
> 	my $class = ref($ref) || $ref;
> 	my $self = {};
> 	bless($self, $class);
> 	return $self;
> }
> sub del {
> 	print "del called\n";
> }
> 1;
> 
> 
> bug.py :
> 
> from perlmod import PerlClass
> 
> bug = PerlClass("bug")()

You probably know that you can also write this as:

  from perlmod import Perl
  bug = Perl.bug()

It looks cleaner to me at least.

> bug.del()

All python method calls can be rephrased in terms of getattr().  It
means you can write it as:

  getattr(bug, "del")()

The perl ref objects also has the feature that if you append "_tuple"
to the method name then the method will automatically be called in
array context.  It means that you can call also call your del method
in this way:

  bug.del_tuple()

Regards,
Gisle Aas,
ActiveState