Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling perl from php?

I have a php script that handles a form input. For design reasons both a bit out of my control, and which I do not entirely wish to change, I have to call a perl script with the parameters specified in the html form.

I sanitized all inputs and then output them to a file called input, which is read by the perl script named, for sake of brevity in this question, script.pl. Script.pl should do some stuff and then write all outputs to a file named output.

I call the perl script from php like so:

system('perl script.pl 2>errors');

No good, nothing happens. output is not created, errors is not created, and the side effect does not occur.

My apache runs as www-data user and group id. My directory is set with 775 settings with ownership as me:www-data. (My user name is replaced with "me" for sakes for privacy).

My question is two fold: 1) Am I doing this wrong? If so how should I improve upon the code? 2) Is there a more sane way to catch errors in system execution?

After programming in perl for a while, php feels like a pain in the ass.

OS: Ubuntu server edition

like image 211
Razor Storm Avatar asked Aug 30 '10 01:08

Razor Storm


1 Answers

popen can be used to get the shell's response. that is your best bet. Which can help you debug why system is angry. also, if your pl is saying "hello" and "bye", popen can even read that.

If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell

Ideally, I would have taken data from stdin and written to stdout. popen would allow neat access to both.

popen('pwd;perl script.pl 2>errors;echo done');

then you can see where were you (directory) when system got called and did it "done".

like image 182
thevikas Avatar answered Sep 24 '22 01:09

thevikas