Can you include PHP in a Perl file?


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.

9/26/2009 6:45:58 PM

Accepted Answer

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 script:

[root@www1 cgi-bin]# cat
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

echo "PHP generated this";


Here's the output of running "":

[root@www1 cgi-bin]# perl
This is a perl script
This is from PHP:
PHP generated this
Back to perl...
1/18/2009 4:54:07 PM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow