By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

Capturing PERL script output into PHP variable

P: n/a
This must be easy, but I'm missing something...
I want to execute a Perl script, and capture ALL its output into a PHP
variable. Here are my 2 files:
-------------------------------------
test3.pl
-------------------------------------
print "PERL Hello from Perl! (plain print)<br>\n";
print STDERR "PERL This is text sent to STDERR<br>\n";
$output="PERL Some output:<br>\n";
for ($i=0; $i<5; $i++) {
print STDOUT "PERL $i: This is text sent to STDOUT<br>\n";
$output .= "PERL line $i of output---<br>\n";
}
print $output;
exit;
-------------------------------------
passthru3.php
-------------------------------------
<?
$command = "/usr/local/bin/perl";
$pgm = "test3.pl";

print "PHP: Before passthru, in PHP...<br>\n";
$result=passthru("$command $pgm your_variable");
print "PHP: After passthru, result=($result)<br>\n";
?>
-------------------------------------
OUTPUT:
-------------------------------------
PHP: Before passthru, in PHP...
PERL Hello from Perl! (plain print)
PERL 0: This is text sent to STDOUT
PERL 1: This is text sent to STDOUT
PERL 2: This is text sent to STDOUT
PERL 3: This is text sent to STDOUT
PERL 4: This is text sent to STDOUT
PERL Some output:
PERL line 0 of output---
PERL line 1 of output---
PERL line 2 of output---
PERL line 3 of output---
PERL line 4 of output---
PHP: After passthru, result=()

I'd like to be able to capture all the output in $result, and only
show it
when I feel like it. The passthru() doesn't interfere, but the
system() command supposedly only shows the last line? What am I
missing?
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Mark Wilson CPU <wi****@cpuworks.com> wrote:
This must be easy, but I'm missing something...
I want to execute a Perl script, and capture ALL its output into a PHP
variable. Here are my 2 files:
-------------------------------------
test3.pl
-------------------------------------
print "PERL Hello from Perl! (plain print)<br>\n";
print STDERR "PERL This is text sent to STDERR<br>\n"; [snip] I'd like to be able to capture all the output in $result, and only
show it
when I feel like it. The passthru() doesn't interfere, but the
system() command supposedly only shows the last line? What am I
missing?


You probably have to redirect the output from stderr to stdout:
$result=passthru("$command $pgm your_variable 2>&1");

Jul 17 '05 #2

P: n/a
wi****@cpuworks.com (Mark Wilson CPU) wrote in message news:<3f**************************@posting.google. com>...
This must be easy, but I'm missing something...
I want to execute a Perl script, and capture ALL its output into a PHP
variable. Here are my 2 files:

<snip>

FWIW, check this <http://www.zend.com/php5/articles/php5-perl.php>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
Jul 17 '05 #3

P: n/a
Carved in mystic runes upon the very living rock, the last words of Mark
Wilson CPU of comp.lang.php make plain:
I want to execute a Perl script, and capture ALL its output into a PHP
variable. $result=passthru("$command $pgm your_variable");


passthru() doesn't return anything. That's what "void" means in the Fine
Manual.

Try http://www.php.net/manual/en/function.exec.php

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #4

P: n/a
wi****@cpuworks.com (Mark Wilson CPU) wrote
in message news:<3f**************************@posting.google. com>...

This must be easy, but I'm missing something...
I want to execute a Perl script, and capture ALL its output into a PHP
variable.
Here's your problem:
$result=passthru("$command $pgm your_variable");


The correct way to use passthru() in your situation would be:

ob_start();
passthru("$command $pgm your_variable");
$result = ob_get_clean();

Now all output of the Perl script is captured in the $result variable.

Cheers,
NC
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.