http://www.perlmonks.org?node_id=750583

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 (Saint) 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.