Solved

How do I pass an array to an xsub?

Posted on 2001-07-03
4
212 Views
Last Modified: 2007-12-19
I want to pass a Perl array as an argument to an xsub subroutine.  The xsub will then pass it on to a C library function that expects int xyz[].  How do I code the xsub?

If you need a more concrete example, what I'm trying to do is make a Perl version of setgroups(), which is int setgroups(size_t size, const gid_t *list) in C.

I can't find any documentation other than overview of the xsub interface and typemap file.
0
Comment
Question by:bryanh
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
Sapa earned 100 total points
ID: 6251005
------------------- xsub code ------------------
int
setgroups(...)
    PREINIT:
       int i;
       gid_t *glist;
    CODE:
    if( items > 0 ) {
       glist = (gid_t *)malloc(items * sizeof(gid_t));
       for (i = 0; i < items; i++)
           glist[i] = SvIV(ST(i));
       RETVAL = setgroups( items, glist );
       free (glist);
    }
    OUTPUT:
    RETVAL
-------------------------------------------------

- Sapa
0
 
LVL 5

Author Comment

by:bryanh
ID: 6251079
That's more of an example of a variable number of scalar arguments.  That's good; I hadn't realized that's what I need for setgroups(), so thanks for that.  But what if I have a single argument that is an array, for example

   sub1($a, @b, $c);

Or is that even meaningful in Perl?
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6252237
"sub1($a, @b, $c);"

In this call:

$a will be in ST(0)
$c will be in ST(items-1)
@b will be in ST(1)..ST(items-2)

because all arguments concatenated into one
list passed to called function.
except if you have defined function
prototype as

sub sub1($\@$)

in this case, there will be 3 arguments
passed scalar, _reference_ to array, and
one more scalar. But it's unnecessary in
your case.

- Sapa

0
 
LVL 5

Author Comment

by:bryanh
ID: 6253230
That concatenation is something I've always suspected but never knew for sure.  Thanks!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question