note 19050 added to function.exec
| From: | marat at provote dot com | Date: | Thu, 14 Feb 2002 00:58:34 +0000 |
| Subject: | note 19050 added to function.exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26596@lists.php.net to get a copy of this message | ||
As we all know, there are often times more ways than one to skin a cat.
The example above is universal in that the logic which supports it would work no matter how many
arrays or hashes you needed to pass back to your PHP script, from the PERL script the PHP executed.
However, if you were passing back strictly arrays, then you might want to try something a little
more effecient, which would not require a 'foreach()' loop to produce the output on the
PERL side.
Passing back one or more arrays in PERL:
<code>
print "\"" . @array . "\" \"(array name)\" @(array name)" ;
</code>
This will produce a 'double-quote space double-quote' delimited string of array values.
The first value in this string will be the number of members in the PERL array, while the second
value is the name of the PERL array. This member number value becomes important on the PHP side,
when we want to do a cursory consistency check on the PHP array.
Grabbing output PERL arrays in PHP:
<code>
exec("path_to_perl_file", $array) ;
while (list($name, $value) = each($array)) {
$temparray = split('" "', $value) ;
$perlarraylength = $temparray[0] ;
$perlarrayname = $temparray[1] ;
unset($temparray[0]) ;
unset($temparray[1]) ;
/* here is where the consistency check comes in */
if ($perlarraylength != sizeof($temparray)) {
echo("corrupt perl to php array translation...") ;}
else { ${$perlarrayname} = $temparray ;}
unset($temparray) ;}
</code>
That's it..., quick and easy.
Unfortunately, PERL hashes can't be handled in this fashion, seeing as how simply printing
"%(hash name)" will yield the hex reference to the hash. Therefore PERL hashes must be
passed back by printing each member through a loop...
Needed to clarify,
MARAT
--
http://www.php.net/manual/en/function.exec.php
http://master.php.net/manage/user-notes.php?action=edit+19050
http://master.php.net/manage/user-notes.php?action=delete+19050
http://master.php.net/manage/user-notes.php?action=reject+19050