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.
| [reply] |
|
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).
| [reply] |
|
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)
| [reply] |
|
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.
| [reply] |
|
| [reply] [d/l] |
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. | [reply] |
|
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.
| [reply] [d/l] [select] |
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... | [reply] |
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. | [reply] |
Re: How to convert a script from C to Perl?
by planetscape (Chancellor) on Oct 26, 2010 at 17:14 UTC
|
| [reply] |
|
| [reply] |
|
| [reply] |
|
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.
| [reply] |
|
|
|
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... | [reply] |
|
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)
| [reply] |
|
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... | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
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
| [reply] [d/l] |
|
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.
| [reply] [d/l] |