Bill Moseley <[EMAIL PROTECTED]> writes:
>I'm working on an updated API for the swish-e search engine (this activity
>may generate a few perl-xs question along the way...) Swish-e is written
>in C.
>
>I'm trying to write a single .xs file that is a collection of packages, and
>I'm not seeing how to call new() in another package from an xsup.
>
>Swish's API is basically:
>
> sw_handle = SwishInit("index.file");
> sw_search = New_Search_Object( sw_handle, "query words");
> sw_results = SwishExecute( sw_search );
>
> while ( (result = SwishNextResult( sw_results )) )
> print result...
>
>
>This lends itself to an OO interface well.
>
>I'm using:
>
>TYPEMAP
>SW_HANDLE * O_OBJECT
>SW_SEARCH * O_OBJECT
>SW_RESULTS * O_OBJECT
>SW_RESULT * O_OBJECT
>
>So in my xsub I have:
>
>MODULE = SWISH::API PACKAGE = SWISH::API
>
>SW_HANDLE
>new(CLASS, index_file_list )
> char *CLASS
> char *index_file_list
> CODE:
> SwishErrorsToStderr();
> RETVAL = SwishInit( index_file_list );
> OUTPUT:
> RETVAL
>
>void
>DESTROY(self)
> SW_HANDLE self
> CODE:
> SwishClose( self );
>
>
>
>
>Then later in the same .xs file but in a new package:
>
>MODULE = SWISH::API PACKAGE = SWISH::API::Search
>
>SW_SEARCH
>new(CLASS, swish_handle, optional_string )
> char *CLASS
> SW_HANDLE swish_handle
> char *optional_string
>
> CODE:
> RETVAL = New_Search_Object( swish_handle, optional_string );
> OUTPUT:
> RETVAL
>
>
>So my question is how do I call SWISH::API::Search->new from within the
>xsub (from within the SWISH::API package)? Do I somehow use call_pv()?
There is not really anything different in XS vs perl - 1st arg
is class name or an object in the class and is PUSHed in normal
way. Then use call_method() passing method name.
So SWISH::API::Search->new(...) becomes:
PUSHMARK;
XPUSHs(sv_2mortal(newSVpv("SWISH::API::Search",18));
...
PUTBACK;
call_method("new",G_SCALAR);
And
$search_object = $swish_handle->new( $query );
becomes :
PUSHMARK;
XPUSHs(swish_handle_sv);
XPUSHs(query_sv);
PUTBACK;
call_method("new",G_SCALAR);
SPAGAIN;
search_object_sv = POPs;
Which suggests that using this "signature" would be better:
SW_SEARCH
new(CLASS, swish_handle, optional_string )
char *CLASS
SV *swish_handle
As it avoids the need to back-convert the SW_HANDLE to an SV.
Things like
call_method("ElseWhere::new",G_SCALAR);
for
$object->ElseWhere::new(...)
work too - but (in general) SUPER:: as a prefix does not work
as the "current package" of XSUB is seldom what is required
(So you have to use the underlying Foo::Bar::Baz::SUPER scheme.)
--
Nick Ing-Simmons
http://www.ni-s.u-net.com/