migrating c code to perl

M

Martien Verbruggen

Is there any way (tool) to migrate/convert c code to perl ?

None that I know of. If there was such a tool, I probably wouldn't use
it, and I certainly wouldn't trust it.

There are many things that work in C, are very idiomatic, but which
are just not the right way to approach things in Perl. There are
things that are possible in C, and simply not possible at all in Perl.

For example, walking a string in C by incrementing a pointer until you
reach a null byte, is normal in C. In Perl, that is not what you do.

Another example is the use of machine-specific register tricks, or
machine-specific address tricks. In (machine-specific variants of) C
you can set a pointer to a fixed address, which is meaningful to the
machine, and then manipulate or read that address. You can't do that
in Perl.

Trying to translate C into Perl on a code line by line basis will
always end up with bad code, and most often with something ugly,
unmaintainable, if it is possible at all.


If you can't rewite the logic in Perl, maybe you should try to make
your C code available as a library, and provide some access points to
this library to Perl, with XS, SWIG, Inline::C or soemthing like that.

The question is: Why do you want to rewrite it? The best option
depends on the answer you give to that question.

Martien
 
C

chetan

i have a tool written in c. Need to have the same functianlities in perl
so that they could be a part of an existing framework which is already
there in perl. I am going through "perlxs and friends" (i am fresh to
the world of perl). was thinking if there are similar utilities which
could help me atain my goal.
I agree with you that most of the aspects couldnt be taken care of
when we want to convert c to perl and you are right, line by line
conversion doesnt make any sense..i agree with you in this...
will check out SWIG Inline and similar stuff.
Thanks for the suggestions :)
 
T

Tassilo v. Parseval

Also sprach chetan:
Is there any way (tool) to migrate/convert c code to perl ?

No, but you can make an XS module that uses the C code base. I did that
once for File::Locate which was the only Perl module so far that merely
took me one afternoon to complete. I simply copied the relevant code
from the GNU findutils and added the necessary stuff from the perlapi.

Perl and C work together exceptionally well, therefore integrating C
into Perl is often a better solution than rewriting (it wont result in a
penalty-hit eithe).

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top