Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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?

Comment on C to perl
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 }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2016-04-29 23:51 GMT
Find Nodes?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW

    Results (441 votes). Check out past polls.