PHPとPerl間での配列の受け渡し
Perlにおける配列は、各種のデータのリストを格納するデータ型です。配列の要素は、文字列、数値、各種のスカラーデータ(他の変数を含む)のいずれでも構いません。Perlでは、配列の要素に0から順に番号が振られます。Perlで配列を宣言するには、次のように記述します。
@arrayname = ("element1", "element2",...,"elementn");
配列名は@記号で始まらなくてはなりません。配列の大きさには制限がありません。@arrayname[subscript]構文を使えば、配列の任意の要素を参照できます。
次の例では、1組の文字列を引数としてPerlスクリプトに渡し、Perlスクリプトはこれらの文字列から配列を作成します。このサンプル(array.php)では3つの文字列を渡します。
<?php $result=shell_exec( "C:\Perl\bin\perl.exe array.pl Mark Jane Martin"); echo($result); ?>
もう1つ例を見てみましょう。リスト2のPerlスクリプト(array.pl)は、Perlの基本的な文字列処理(要素の追加、要素の削除、配列から文字列への変換、文字列から配列への変換、配列のソート)を実行します。
$a=$ARGV[0];
$b=$ARGV[1];
$c=$ARGV[2];
#Define an array
my @PERLarray = ($a, $b, $c);
#Print the whole array
print "The PERL array is : @PERLarray <br />";
#Print each scalar element
print "Print each element of the array : <br />";
foreach $PERLarray (@PERLarray)
{
print "$PERLarray"."<br />";
}
#Add elements at the end of the array
print "Adding one element at the end : ";
push(@PERLarray, "Mary");
print "@PERLarray"."<br />";
#Add elements at the end of the array
print "Adding one element at the beginning : ";
unshift(@PERLarray, "Tom");
print "@PERLarray"."<br />";
#Remove the last element of the array
print "Remove the last element of the array : ";
pop(@PERLarray);
print "@PERLarray"."<br />";
#Delete element by an index number
#delete(@PERLarray[2]);
#print "@PERLarray"."<br />";
#Transform array to string
print "Transforming the array into string : ";
$PERLstring = join(':',@PERLarray);
print "$PERLstring"."<br />";
#Transform strings to array
print "Transforming the string into array : ";
@Newarray = split(':',$PERLstring);
print "@Newarray"."<br />";
#Sorting an array
print "Sorting the array : ";
@Newarray = sort(@Newarray);
print "@Newarray"."<br />";
このarray.phpページをブラウザで開くと、次のような出力結果が表示されます。
The Perl array is : Mark Jane Martin Print each element of the array : Mark Jane Martin Adding one element at the end : Mark Jane Martin Mary Adding one element at the beginning : Tom Mark Jane Martin Mary Remove the last element of the array : Tom Mark Jane Martin Transforming the array into a string : Tom:Mark:Jane:Martin Transforming the string into array : Tom Mark Jane Martin Sorting the array : Jane Mark Martin Tom
引数のリストではなくPHP配列をPerlに渡すには、次のスクリプト(arraytoString.php)に示すように、最初にPHP配列を区切られた文字列に変換したうえで、その文字列を渡します(任意の区切り文字が使えます。この例ではコロン「:」を使っています)。
<?php
$nr = array("one", "two", "three", "four", "five");
//Convert from PHP array into PHP string
$separated = implode(":", $nr);
echo 'The PHP array converted into a string: <br />';
echo $separated."<br />";
//Execute the arraytostring.pl Perl script
$result=shell_exec("C:\Perl\bin\perl.exe arraytoString.pl ".$separated.' " ');
?>
次のPerlスクリプト(arraytoString.pl)は、区切られた文字列の引数から配列を作成します。
#print "Hello from ActivePerl!\n";
$PHPstring=$ARGV[0];
#Convert the string into a Perl array
@token= split(/:/, $PHPstring);
#Display the array
#print "The Perl array is : @token <br />";
#Convert the token array into a string
$Perlstring = join('//',@token);
print "$Perlstring";
上のPerlスクリプトは、Perlスクリプトが実行されたことを示すフラグとして文字列"Perl"を配列の全要素に付加します。
結合された文字列をPHPに返し、さらに、区切られたPerl文字列から配列を再構築もできます。
//Convert from Perl string to PHP array
$pieces = explode("//", $result);
//Listing the PHP array
echo "<br /><br /> Convert from Perl string to PHP array: <br />";
echo "pieces[0]=".$pieces[0]."Perl <br />";
echo "pieces[1]=".$pieces[1]."Perl <br />";
echo "pieces[2]=".$pieces[2]."Perl <br />";
echo "pieces[3]=".$pieces[3]."Perl <br />";
echo "pieces[4]=".$pieces[4]."Perl <br />";
?>
このスクリプトは、explodeメソッドを使って、区切られた文字列をPHP配列に変換します。出力結果は次のとおりです。
Convert from Perl string to PHP array: pieces[0]=onePerl pieces[1]=twoPerl pieces[2]=threePerl pieces[3]=fourPerl pieces[4]=fivePerl
