Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

XS Library - Embedding C code in Perl

by ginttu (Sexton)
on Nov 13, 2002 at 22:31 UTC ( #212712=perlquestion: print w/ replies, xml ) Need Help??
ginttu has asked for the wisdom of the Perl Monks concerning the following question:

Hey! I am embedding some code written in C into Perl by the use of H2XS. I've been able to write little C programs like "Hello, World" and have the output come out of h2xs.
############### # C code # Hello, World void print_hello (void) { printf("Hello, World\n"); } ############### # XS code # Hello, World #include "EXTERN.h" #include "perl.h" #include "XSUB.h" MODULE = Hello PACKAGE = Hello void print_hello()

Now the next C program I would like to use is very complex! Does anyone know how to pass parameters from my command line so I can use them with my *.xs code? Any help would be appreciated!! Also, does anyone have any examples using h2xs with C libraries?? Thanks, Jennifer

Comment on XS Library - Embedding C code in Perl
Download Code
Replies are listed 'Best First'.
Re: XS Library - Embedding C code in Perl
by Zaxo (Archbishop) on Nov 14, 2002 at 00:11 UTC

    Inline is fine, but it is perhaps not as well integrated into ExtUtils and module building as the standard XS interface.

    To answer your question directly, you should #include the perl headers before your C code, unless there is good reason not to. That gives you macros and functions to ease writing your functions . This example is adapted from perlxstut, which I recommend to you:

    MODULE = Foo PACKAGE = Foo int is_even(input) int input CODE: RETVAL = (input % 2 == 0); OUTPUT: RETVAL

    I'd parse the command line in perl before calling library functions with the arguments.

    To link to an external lib, #include <foo.h> (its header) in the xs file, and put the linker directive '-lfoo' in Makefile.PL's LIB parameter.

    After Compline,
    Zaxo

Re: XS Library - Embedding C code in Perl
by talexb (Canon) on Nov 13, 2002 at 22:34 UTC

    The stock answer for this question is "Use Inline::C instead". Having never used either, I can't base my recommendation on any experience .. just on what I've heard.

    --t. alex
    but my friends call me T.
      I would concur with the comments of talexb above - I have found Inline::C to make incorporating C and Perl code together incredibly easy.

      The only thing which I would add to talexb's post is a few links for further information:

       

      perl -e 'print+unpack("N",pack("B32","00000000000000000000000111011110")),"\n"'

Re: XS Library - Embedding C code in Perl
by tachyon (Chancellor) on Nov 13, 2002 at 23:40 UTC
Re: XS Library - Embedding C code in Perl
by Elian (Parson) on Nov 14, 2002 at 00:19 UTC
    Command line args are in the perl variable @ARGV, which is in the main package. You can go wading through the symbol tables for it, but it'd probably be easier to pass in a reference to it from your perl code, in case you want to use the routine on an alternate array or something.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://212712]
Approved by RollyGuy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2015-07-29 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls