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

How to convert a script from C to Perl?

by Anonymous Monk
on Oct 26, 2010 at 14:19 UTC ( [id://867478]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm a graduate student in Plant Sciences and I need a Perl solution to an actual real-life problem. I've been given permission to use the following script and I want to get it up and running on a web server at the University of Florida: http://www.colby.edu/info.tech/BI211/plant_id.c

Unfortunately, the script seems to be written in C or C Sharp and it won't run on our server unless it's written in Perl. Can anyone give me some ideas about how to proceed?

It doesn't seem like there's a whole lot that needs to be converted there. It's a very simple script and most of the page is taken up with data and a form. Is this something I can convert myself? Do I find a programmer in the yellow pages? Any guidance or suggestions would be welcome!

  • Comment on How to convert a script from C to Perl?

Replies are listed 'Best First'.
Re: How to convert a script from C to Perl?
by Marshall (Canon) on Oct 26, 2010 at 15:58 UTC
    You have a number of problems with this code:

    1) You need to get the source for util.c as presumably fmakeword(), plustospace(), unescape_url() are in there.

    2) Past that, the code just will not work. For example, the subroutine num2Pstring() returns a pointer to a dynamically allocated stack variable - that just will not fly - barf, die, croak. So the assumption that you have "good" C code is wrong.

    3) You might be better off generating a program spec of what you want this thing to do and having somebody write a new version of it for you.

      I agree with Marshall, you need to have the code for num2Pstring fixed, this is very broken. Besides, it seems to be a bad reinvention of the sprintf function.

      The source of util.c is on the same web server (simply tried it, seems to work).

      After that, compiling the program is by far the easiest option and that is not very difficult if you know how.

      Converting this program to Perl is not going to change anything: the c-program works as a CGI program (I can see that from the code), for which the web server has to be configured by the sysadmin. If you have your program converted to Perl, you would still have to run it as a CGI program (there are different options, but those are waaay more difficult).

      Thanks for taking the time to look at the program so carefully! That's very helpful.

      Some version of the code appears to be working at Colby (because they have a working website using it) but, based on what you've said, it appears that the real code is different from what they posted on that page.

      I'll make one more attempt to get the correct code from Colby and then I'll give up and hire a programmer like you suggest.

      (original poster)

        The program is quite simple and easy to translate, for someone that know C and Perl. As an exercise, I wrote a translation. It is very quick and dirty and I wouldn't run it on a public server without further review, but it might get you going. It's a bit too big to post here, but you can download it from my scratchpad. I'll leave it there for a few days.

        but, based on what you've said, it appears that the real code is different from what they posted on that page.

        It is possible for the code to run correctly even with that nasty bug.

        It definitely does not hinder a translation to Perl. The direct translation «sub { my $x; ... return $x; }» isn't buggy.

Re: How to convert a script from C to Perl?
by jettero (Monsignor) on Oct 26, 2010 at 14:23 UTC
    That's a pretty small program. If you learned some Perl and some C, I'm sure you could figure it out on your own. It sounds like you'd really rather hire someone though.

      http://jobs.perl.org/ <-- Might be a handy link.

      You could certainly get that script done in house if you have anybody with available time. Just make sure that whomever does the conversion has use strict; at the top, or you will regret it later.

Re: How to convert a script from C to Perl?
by choroba (Cardinal) on Oct 26, 2010 at 14:27 UTC
    Have you tried the Search? C to perl contains some answers, even if not the ones you would like to see...
Re: How to convert a script from C to Perl?
by ww (Archbishop) on Oct 26, 2010 at 14:42 UTC
    With minimal learning, you or a colleague may be able to use Inline::C (without touching the Colby source code). The documentation is on CPAN.

    If your university site requires that CGI's be Perl, it probably has someone who can do the job.

Re: How to convert a script from C to Perl?
by planetscape (Chancellor) on Oct 26, 2010 at 17:14 UTC

    What does your campus IT shop have to say about the matter?

    HTH,

    planetscape

      That’s a very good suggestion.   Follow-up on it right away.   Other academic departments might be able to help you, too.   Maybe there’s a graduate student in the computer-science department ... and a professor who can assign him/her a little (more...) “grunt work” to do on your behalf.

      Here's the official word from my campus IT shop: "I am very sorry but we do not have a platform where you can compile a C program and then run it as a cgi on our servers. Our Apache web hosting infrastructure supports php, perl and python. It does not support C."

      So... javascript, php, perl, or python? Any advice?

      (original poster)

        Our Apache web hosting infrastructure supports php, perl and python. It does not support C.

        I would guess that this rules out Javascript.

        Also, this site's name being Perlmonks, I'm not sure what kind of answer you expect. Personally, I would make the answer depend on local factors, like availablility of local people who understand the target language. A target language is usually easier to change than the people you have available for support.

Re: How to convert a script from C to Perl?
by Anonymous Monk on Oct 26, 2010 at 14:33 UTC
    Before going to the trouble of rewriting something that already works in another language, I would first try to make sure you absolutely can't execute binaries as CGI programs on your server (very unlikely).

    Of course, you can't run the C source directly (as with Perl)... You'd have to compile it first. But looking at the given program, getting it to compile looks like much less trouble than rewriting it...

      Hmm... I don't know the first thing about binaries or compiling sources, but you make it sound like there may be cause for hope. I'll pursue this angle.

      (original poster)

        The general procedure would be to put the required files (plant_id.c, util.c, util.h) into some directory, and then run a C compiler:

        $ cc plant_id.c -o plant_id plant_id.c: In function ‘num2Pstring’: plant_id.c:839: warning: function returns address of local variable
        This should create a binary named plant_id, which you'd copy into the cgi-bin directory of the webserver, making sure the executable bit of the file is set (chmod +x plant_id).

        This of course assumes you have a C compiler installed, like gcc on Linux. Also, the machine you compile the program on should be binary compatible with the target machine (the webserver) - The fewest potential problems would arise if you could compile it on the server itself.

        The warning "function returns address of local variable" issued by the compiler (see above) refers to the issue Marshall mentioned, which should definitely be fixed...

Re: How to convert a script from C to Perl?
by JavaFan (Canon) on Oct 26, 2010 at 14:55 UTC
    Unfortunately, the script seems to be written in C or C Sharp and it won't run on our server unless it's written in Perl.
    Considering that Perl is executed by perl, which is written in C, that's actually quite an odd statement.
    Is this something I can convert myself?
    How should we know? Can you? Have you tried?

    I would probably just compile the program, and then run system or qx to run the executable. Or turn the program into a library, and link it to my Perl program using XS.

Re: How to convert a script from C to Perl?
by Anonymous Monk on Mar 11, 2018 at 13:35 UTC
    void swap(char *x, char *y) { char temp; temp = *x; *x = *y; *y = temp; } void permute(char *a, int l, int r) { int i; if (l == r) printf("%s\n", a); else { for (i = l; i <= r; i++) { swap((a+l), (a+i)); permute(a, l+1, r); swap((a+l), (a+i)); //backtrack } } }

    2018-03-11 Athanasius added code tags

      Here's one possible solution. No attempt was made to match the order in the generated sequence.

      use Algorithm::Combinatorics qw( permutations ); my $iter = permutations( [split //, shift // q(how do you do)] ); while (my $p = $iter->next) { print join q(), @$p, "\n"; }

      See Algorithm::Combinatorics for more info and examples.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://867478]
Approved by jettero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found