2

I have a perl script that has an endless loop that reads an integer from the user and add it to the variable $b every time;

$b = 0;
while ( 1 == 1 ) {
    $a = <STDIN>;
    $b = $b + $a;
    print $b + "\n";
}

I have a php form that has an input-text field and submit button, and when pressing the submit button I want to pass the value given in the input-text field to the running perl script and get the last value of the $b variable to show it in my php form.

So my question is how to do this interconnection between php and perl?

LF00
  • 27,015
  • 29
  • 156
  • 295
VFX
  • 496
  • 4
  • 13

2 Answers2

0

You can open a pipe to a new instance of your Perl program with popen

$pipe = popen("/path/to/program.pl", "w");

then you can write your numbers to the pipe and the Perl code will add then up

fwrite($pipe, "99\n");
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • how do I get the value of $b back to php using the $pipe ? – VFX May 06 '17 at 13:15
  • @VFX: That is a different problem and you need to open a new question. The `$pipe` connects to the Perl program's stdin, so you can't read from it. You will need to use `proc_open` instead. Why do you need a Perl program to add numbers up for your PHP code? – Borodin May 06 '17 at 13:19
  • Actually, I don't need a Perl script to add numbers up, but this example is simple to ask my question, the main idea is the interconnection between php and perl – VFX May 06 '17 at 13:25
0

Both perl and php can do IPC via UNIX sockets, message queues and FIFO and much more. Connecting to an already running process STDIN - if it exists and is a named device/object - is not so easy.

ulix
  • 274
  • 1
  • 2
  • 13