Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I pass an array to an xsub?

Posted on 2001-07-03
4
Medium Priority
?
217 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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

609 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