Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you include PHP in a Perl file?

Tags:

php

perl

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.

like image 869
Daniel Beder Avatar asked Nov 27 '22 19:11

Daniel Beder


1 Answers

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...
like image 109
rjamestaylor Avatar answered Dec 06 '22 05:12

rjamestaylor