sgc has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I was wondering if it's possible to use a c executable within a Perl script. An example might be to manipulate an array using a c executable, say sort the array , then return that sorted array for further usage within the Perl code. Thank you, sgc

Replies are listed 'Best First'.
Re: Using c executable in Perl Script
by gaal (Parson) on Sep 19, 2004 at 16:32 UTC
Re: Using c executable in Perl Script
by The Mad Hatter (Priest) on Sep 19, 2004 at 16:31 UTC
    XS. Look at perldoc perlxs. However, you can supply your own sort routines written in Perl to Perl's sort function.
      Thank you
Re: Using c executable in Perl Script
by dragonchild (Archbishop) on Sep 19, 2004 at 18:50 UTC
    If you can, you most certainly want to use Inline::C for most usages. XS is the traditional way to go, and is best for distributions, but Inline::C works like a charm. I had it up and running in 5 minutes.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Using c executable in Perl Script
by tachyon (Chancellor) on Sep 20, 2004 at 00:07 UTC

    The easiest way to access arrays and hashes when you are using Inline C or XS is to pass by reference and use the access macros to get at the values. Here is a trivial example to get you started.

    use Inline 'C'; print multiply( [ 1,2,3,4 ] ); __END__ __C__ double multiply( SV * terms ) { I32 numterms = 0; int i; double result; /* Make sure we have an array ref with values */ if ((!SvROK(terms)) || (SvTYPE(SvRV(terms)) != SVt_PVAV) || ((numterms = av_len((AV *)SvRV(terms))) < 0)) { return 0; } /* Set result to first value in array */ result = SvNV(* av_fetch((AV *)SvRV(terms), 0, 0)); for (i = 1; i <= numterms; i++) { result *= SvNV(* av_fetch((AV *)SvRV(terms), i, 0)); } return result; }

    cheers

    tachyon

      Thank you Tachyon and to everyone who helped to shed some light on my question about C embedded into Perl. I was not aware of the tools available for working with C code, and you have all taught me some good things. I have to admit , I am probably a bit of an oddballish programmer. I start my apps with a picture in mind , and procede to develop a tool in somewhat bruteforce fashion, which in the end does what's required of it, looks and feels good on the outside. I know Perl better now than any other programming language and have developed some powerful tools using Perl/Tk. I've read "C" and understand the fundamentals of the language but have not programmed extensively with it. I used to write code using Unix C-Shell & nawk but am trying to replace all that with Perl now. In the beginning I wondered just how much can one do with Perl/Tk. Initially , it seemed to me that it would be a limited tool in terms of creating GUI's and interacting with various data , but I have been able to "craft" the pictures in my mind into code quite nicely over time.... What are Perl's limits ? Are there things one can do using C / C++ say that could not be done using Perl ? Cheers, sgc

        You can do almost anything in Perl but some things work better in C. Operating systems and device drivers are two examples. Encryption algorithms are an example where the Perl C XS interface is useful. The main code is C with a Perl wrapper. While you can implement almost any algorithm in Perl, it often makes more sense to wrap well tested, and freely available, C library code. That way you get easy access from Perl and don't have to reininvent the wheel.

        cheers

        tachyon