[% setvar title A method of allowing foreign objects in perl %]
|Note: these documents may be out of date. Do not use as reference!|
To see what is currently happening visit http://www.perl6.org/
A method of allowing foreign objects in perl
Maintainer: Dan Sugalski <firstname.lastname@example.org> Date: 2 Aug 2000 Mailing List: email@example.com Number: 32 Version: 1 Status: Developing
Currently it's a darned pain to deal with objects created in other languages in perl. It would be very nice to be able to call a function written in some other language, take the returned object, and treat it as a perl object. (Which means making method calls and such on it)
Everybody and their dog has written an object-oriented language at this point, and it'd be nice to be able to use objects created by those languages as if they were real perl objects. This would make interfacing perl and OO languages easier, and make things much nicer for embedded and embedding code.
Since writing half a zillion perl2WhoKnowsWhat modules is going to be unweildy, not to mention different for each and every object type, perl punts. We require that the code we interface to defines a dispatch function for each object, and we then just call the dispatch function with enough information for it to Do The Right Thing.
>From the perl programmer's point of view, foreign objects and native objects look the same. Perl will define some generic conversion routines (to int, float, and string) which the foreign code is welcome to override if it wishes.
A scalar is made an object via a call into the perl library. The scalar is marked as an object and stuck into a package. Attached to the scalar is a pointer to the native object, a pointer to a generic dispatch routine, and a pointer to the native destruction routine for the object.
Native code would create the object thusly:
TheObj *foo; SV *new_sv; foo = new TheObj("A parameter"); sv = perl_new_sv(); perl_make_sv_object(sv, "Some::Package", foo, &dispatch_routine, &destroy_routine); perl_return(perl_make_ref(sv));
When you make a method call in perl, like so:
perl calls the underlying dispatch routine to do the actual work. The dispatch routine has a function signature like so:
int status = dispatch(void *native_obj, sv *perl_scalar, char *method_called, int *num_args_in, perl_arg_stack *arg_stack, int *num_args_out, perl_arg_stack *return_values);
None I can think of
 Like, say, C++ or Scheme