tags:

views:

1602

answers:

4

I have a page written in Perl by someone else. I don't know Perl, so I wrote a PHP file that right now just links from the Perl page. What I'd like to do is embed the PHP file in the Perl file if the perl page has been passed a certain variable. If I were using PHP for both I could just do

if ($_GET['sidebar']) include "embedded.php";

I know there are ways to read text files in Perl, but can I include a PHP file within a Perl file?

I'm assuming it wouldn't work because they're processed by different parts of the server, so no high hopes, but maybe someone's tried something like it.

+8  A: 

There are the PHP and PHP::Interpreter modules, both have an include method. Haven't tried either of them myself though.

Leon Timmermans
A: 

You could check out this project:

http://search.cpan.org/~esummers/PHP-Include-0.2/lib/PHP/Include.pm

HP::Include builds on the shoulders of Filter::Simple and Parse::RecDescent to provide a Perl utility for including very simple PHP Files from a Perl program.

When working with Perl and PHP it is often convenient to be able to share configuration data between programs written in both languages. One solution to this would be to use a language independent configuration file (did I hear someone say XML?). Another solution is to use Perl's flexibility to read PHP and rewrite it as Perl. PHP::Include does the latter with the help of Filter::Simple and Parse::RecDescent to rewrite very simple PHP as Perl.

Filter::Simple is used to enable macros (at the moment only one) which cause PHP to be interpolated into your Perl source code, which is then parsed using a Parse::RecDescent grammar to generate the appropriate Perl.

PHP::Include was designed to allow the more adventurous to add grammars that extend the complexity of PHP that may be included.

splattne
That only supports a small subset of PHP, it is likely not to be enough.
Leon Timmermans
+7  A: 

If you simply want to include the resulting output (HTML, etc.) of the PHP script into the perl-generated page you can use backticks to call either the PHP script via php-cli, like this:

First, the test.pl script:

[root@www1 cgi-bin]# cat test.pl
#!/usr/bin/perl
print "This is a perl script\n";
my $output = `php test.php`;
print "This is from PHP:\n$output\n";
print "Back to perl...\n";
exit 0;

Next, the test.php script:

[root@www1 cgi-bin]# cat test.php
<?php

echo "PHP generated this";

?>

Here's the output of running "test.pl":

[root@www1 cgi-bin]# perl test.pl
This is a perl script
This is from PHP:
PHP generated this
Back to perl...
rjamestaylor
A: 

Perl interpreter can execute Perl syntax commands, despite there are some similarities between Perl and PHP syntax, you will need a PHP interpreter to run PHP code. Instead you can use some Perl commands to use output of a PHP script. you should execute the PHP script using the system PHP interpreter in your Perl code, and then use the output of that execution. your PHP code needs to be some command line code that will return (like echo or print) the output. if you need to pass arguments to the PHP code, use command line arguments when executing the PHP script in your Perl code. so your Perl code will be like this:

 // your code before PHP call
 $php_output = `php /path/to/php/script -param1 -param2 -param3`;
 // your code after PHP call

and your PHP script can receive parameters like this:

<?php
// argv() is defined by PHP and contains all command line params
$params = argv();
// first index of argv is the name of the PHP script you are running.
array_shift($params);
// now you have all parameters.
// your code here generates some results.
echo $resutls;
?>

I'm not a Perl programmer, so there might be other (better) ways to execute a external program in Perl. but the general steps are like this.

farzad