Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

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,

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://212712]
Approved by RollyGuy
[TCLion]: I am breaking down the line and putting the date in the correct order during output
[TCLion]: at least that's the plan
[1nickt]: Corion this looks good, from the pod: "there are 9000+ variations that are detected correctly in the test files (see t/data/* for most of them). If you can think of any that I do not cover, please let me know."
[TCLion]: some moron put the date like this : Mon Feb 20 09:31:30 2017
[Corion]: 1nickt: Yes, the module sounds promising indeed
[1nickt]: "putting the date in correct order" how?
[Corion]: TCLion: Whee ;)
[TCLion]: need to put like this : 2017-02-20 09:30:53
[1nickt]: That's why I asked if you are using DateTime. It has a large number of supporting modules (the author likes the term 'eco-system') so if you are already creating a DateTime obj from your dates, this module would read in the mnoron-formatted 1s seamlessly
[TCLion]: when I put the date together it looks like : 2017-Feb-24 (month is the problem)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (14)
As of 2017-03-23 14:53 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (288 votes). Check out past polls.