Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

C to perl

by jonam (Acolyte)
on Mar 14, 2009 at 07:25 UTC ( #750583=perlquestion: print w/replies, xml ) Need Help??
jonam has asked for the wisdom of the Perl Monks concerning the following question:

I have a C program and I want to convert that into perl code(automated). How can I do that?

Replies are listed 'Best First'.
Re: C to perl
by ig (Vicar) on Mar 14, 2009 at 08:57 UTC

    Or maybe you want Inline::C so you can call your C code from perl without having to translate it.

Re: C to perl
by GrandFather (Sage) on Mar 14, 2009 at 08:52 UTC

    One line at a time, except sometimes. What have you tried and where are you having trouble?

    Perl inherits a lot of syntax from C so a lot of stuff translates fairly easily.

    Update: please indicate any substantive edits to your node using an update (like this one). Inserting '(automated)' substantially alters your question. Replying to one of the replies you received would have been better in this case.

    True laziness is hard work
Re: C to perl
by bart (Canon) on Mar 14, 2009 at 12:52 UTC
    What kind of program? If it's some kind of library, it might indeed make sense to leave at least some functions in C, and write an XS wrapper for them (that, or Inline::C, which does the hard work for you). At least you're already confident that the functions in C already work.
Re: C to perl
by Porculus (Hermit) on Mar 14, 2009 at 21:28 UTC

    You can't. The semantics of Perl and C are sufficiently different that there is no simple way to automatically convert one into the other -- and certainly there is none that would output readable and idiomatic code.

    The best way forward depends on the outcome you want. It might be appropriate to wrap parts of the C code in XS or to incorporate them with Inline::C, as others have suggested above; or (given that XS can be somewhat baroque) it might be simpler to have a separate C program that your Perl program interfaces with through some IPC mechanism.

    But if what you actually need is to end up with a pure-Perl program that does exactly the same thing as the existing C program, without itself using any C code at all, then the only satisfactory way to get that is to have someone who is experienced with both C and Perl sit down, read the C, understand the algorithms it's using, and reimplement those in Perl from scratch.

Re: C to perl
by Anonymous Monk on Mar 15, 2009 at 03:47 UTC
      How could I have missed that :)
Re: C to perl
by educated_foo (Vicar) on Mar 15, 2009 at 01:40 UTC
    sub perlify { <<EOS; use Inline C => <<'EOC'; @_ EOC main(); EOS }
Re: C to perl
by Anonymous Monk on Jul 27, 2016 at 08:51 UTC
    include <stdio.h> int main() { char ch; printf("Enter a character\n"); scanf("%c", &ch); if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || + ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') printf("%c is a vowel.\n", ch); else printf("%c is not a vowel.\n", ch); return 0; }

      Why would you expect a Monk to do this trivial transformation for you? This is not a code-writing service... but posting code (without an explicit question other than the inherited title which lacks the OP's addition of "automated" to the requirement) shows no inclination to use SOPW as a learning tool.

      If you do have an interest in learning, then please read the relevant introductory selections (include those re regexen) in the Tutorials section here.

      UPDATED: Revised para 1 upon discovery that the node to which I replied was a continuation of a 7 year old (2009) thread. Aaargh!


      Another gimmé request?
      Sorry, we expect SOPW to seek wisdom, not to ask us to do so for them.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://750583]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-06-25 02:44 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (564 votes). Check out past polls.