PHP::Include

inkdroid on 2002-12-30T18:52:21

Kris Boulez of london.pm wrote a few weeks ago about parsing simple PHP from Perl, which got me to thinking about a module that would allow you to include PHP files from Perl. This led to PHP::Include which was uploaded to CPAN yesterday.

At the moment it only allows you to include very simple PHP: scalar, array and hash assignments. But it could be useful for Perl applications that need to share configuration data with PHP. The real work is done by Filter::Simple and Parse::RecDescent. PHP::Include was designed so that the more adventurous/knowledgeable could extend the grammar to parse more of PHP.

So given a PHP file "file.php" which contains:
<?php

$robot = 'Book Agent';
$port = 80;
$hosts = Array(
   'www.amazon.com' => 'Amazon',
   'www.bn.com' => 'Barnes and Noble',
   'www.bookpool.com' => 'BookPool'
);
$times = Array( 10,12,14,16,18 );

?>

...and a Perl program like this

use PHP::Include;
include_php_vars( 'file.php' );

The Perl code will be rewritten to be:

use PHP::Include;
my $robot = 'Book Agent';
my $port = 80;
my %hosts = (
   'www.amazon.com' => 'Amazon',
   'www.bn.com' => 'Barnes & Noble',
   'www.bookpool.com' => 'BookPool'
);
my @times = ( 10,12,14,16,18 );

One nice thing is that PHP::Include established the PHP top level namespace on CPAN; so there is a nice home for new modules that glue the two languages together if people are interested.