Solved

How do I pass an array to an xsub?

Posted on 2001-07-03
4
210 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add additional column to .csv using Perl. 8 129
Perl passing in variables to do substitution 6 66
Ned Perl Snippet to Read Files in Directory 5 62
Perl Untar File 1 27
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now