Solved

How do I pass an array to an xsub?

Posted on 2001-07-03
4
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Scripting from a shell script with if and for 4 87
syslog unix file 20 79
Replace  text in a file 2 124
Perl Snippet to Parse String 1 39
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

733 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