Solved

How do I pass an array to an xsub?

Posted on 2001-07-03
4
208 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
Comment Utility
------------------- 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
Comment Utility
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
Comment Utility
"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
Comment Utility
That concatenation is something I've always suspected but never knew for sure.  Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

7 Experts available now in Live!

Get 1:1 Help Now