PerlとPHPの対応表

現在、僕がメインで使おうとしているのはPHPですが、たま~にPerlで書かれた古いプログラムをメンテナンスしなきゃ行けないときもあったりするわけです。そんなとき、自分の知っているPHPのコードがPerlで何に当たるのかが分かれば作業も早いですよね。

PerlとPHPの対応表。
Perl to Php Translation
より一例を紹介。

— Perl arrays —

@a = ();

@a = ( ‘xx’, 11, 33.5, );

@a = 12..33;

$a[2] = ‘something’;

$len = scalar(@a);

# or

$len = @a;

@a3 = (‘xx’, @a1, @a2);

($x, $y) = @a;

$a[@a] = ‘new’; # push

push

pop

shift

unshift

splice

foreach $i (@a) { .. }

— Php arrays —

$a = array();

$a = array( ‘xx’, 11, 33.5, );

$a = range(12,33);

$a[2] = ‘something’;

$len = count($a);

$a3 = array_merge(‘xx’, $a1, $a2);

list($x, $y) = $a;

$a[] = ‘new’; # push

array_push

広告

array_pop

array_shift

array_unshift

array_splice

foreach ($a as $i) { .. }

— Perl MySQL database access —

use DBI;

$dbh = DBI->connect(

‘DBI:mysql:test:localhost’,

$usr,$pwd

);

$dbh->do( $sql_op )

$query = $dbh->prepare( $sql_op );

$query->execute();

while(

@record = $query->fetchrow() )

{ .. }
$dbh->quote($val)

— Php MySQL database access —

$dbh = mysql_connect(

‘localhost’, $usr, $pwd

);

mysql_query(‘USE test’)

mysql_query( $sql_op );

$results = mysql_query( $sql_op );
while($record =

mysql_fetch_row($results))

{ .. }
“‘” . addslashes($val) . “‘”

About: dacelo


Leave a Reply

Your email address will not be published. Required fields are marked *