Click here to get back home

named arguments to XS function?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
named arguments to XS function? gabe 03-24-2006
Posted by gabe on March 24, 2006, 2:44 pm
Please log in for more thread options


hi there,

i'm wondering if it's possible to pass named arguments to an XS
function like so:

$myObj = Package::Bla->new(arg1=>"hello", arg2=>"world");

in XS i'm not sure what to ask for on the argument list. if i just take
an HV*, it doesn't work, an SV* also doesn't work. any ideas?

thanks,
Gabe Schine


Posted by Sisyphus on March 24, 2006, 7:45 pm
Please log in for more thread options



> hi there,
>
> i'm wondering if it's possible to pass named arguments to an XS
> function like so:
>
> $myObj = Package::Bla->new(arg1=>"hello", arg2=>"world");
>
> in XS i'm not sure what to ask for on the argument list. if i just take
> an HV*, it doesn't work, an SV* also doesn't work. any ideas?
>

If you pass those arguments to new(), it will be receiving a list of the 4
strings "arg1", "hello", "arg2", "world" - in that order.

You can have new receive those 4 args as either 'char*' or 'SV*'. In either
case perl's typemaps will handle them correctly for you.

If you want to deal with the 'HV*' then the function needs to be passed a
reference to a hash. (Similarly, if the function returns a HV*, it's
returning a reference to a hash.)

The following Inline::C scripts demonstrates those points:

use warnings;

use Inline C => Config =>
BUILD_NOISY => 1;

use Inline C => <<'EOC';

void foo1 (SV * key1, SV * val1, SV * key2, SV * val2) {
printf("%s %s %s %s\n", SvPV_nolen(key1), SvPV_nolen(val1),
SvPV_nolen(key2), SvPV_nolen(val2));
}


void foo2 (char * key1, char * val1, char * key2, char * val2) {
printf("%s %s %s %s\n", key1, val1, key2, val2);
}

HV * foo3 (HV * x) {
return x;
}

EOC

$hashref = {(arg1 => 'hello', arg2 => 'world')};

foo1(arg1 => 'hello', arg2 => 'world');
foo2(arg1 => 'hello', arg2 => 'world');

$copy = foo3($hashref);

print $copy->, " ", $copy->, "\n";

__END_

Cheers,
Rob



Posted by gabe on March 27, 2006, 5:54 pm
Please log in for more thread options


thanks!

worked like a charm. makes a lot of sense, too, knowing how perl does
arguments.


Similar ThreadsPosted
WWW::Mechanize "There is no Form named (...)" December 8, 2004, 5:58 am
XML::XPath delete function November 26, 2007, 3:35 pm
Soap Lite - raw XML passed to function October 24, 2006, 1:35 pm
Can't call method on an undefined value at June 27, 2005, 2:56 pm
UNIVERSAL::can function failing under heavy load August 25, 2005, 11:56 am
Curses::UI::Widget - binding function keys July 18, 2006, 1:53 am
Problem in accessing DLL function from perl module. July 31, 2007, 4:52 am
Using external REXX function libraries with PERL October 1, 2007, 2:40 pm
Parsing function args in Getopt::Long style January 11, 2008, 4:11 pm
Apparent bug in Win32::API if function returns a float or a double May 22, 2008, 12:06 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap