For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > March 2006 > named arguments to XS function?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author named arguments to XS function?
gabe

2006-03-24, 7:00 pm

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

Sisyphus

2006-03-24, 7:00 pm


"gabe" <gabe.schine@gmail.com> wrote in message
news:1143229483.540247.290050@i40g2000cwc.googlegroups.com...
> 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->{arg1}, " ", $copy->{arg2}, "\n";

__END_

Cheers,
Rob


gabe

2006-03-27, 6:57 pm

thanks!

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com