简体   繁体   English

你可以在Perl文件中包含PHP吗?

[英]Can you include PHP in a Perl file?

I have a page written in Perl by someone else. 我有一个由其他人用Perl编写的页面。 I don't know Perl, so I wrote a PHP file that right now just links from the Perl page. 我不知道Perl,所以我写了一个PHP文件,现在只是来自Perl页面的链接。 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. 如果perl页面已经传递了某个变量,我想要做的是将PHP文件嵌入到Perl文件中。 If I were using PHP for both I could just do 如果我正在使用PHP,我可以做到

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? 我知道有办法在Perl中读取文本文件,但是我可以在Perl文件中包含PHP文件吗?

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. 我认为它不起作用,因为它们是由服务器的不同部分处理的,所以没有很高的期望,但也许有人尝试了类似的东西。

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: 如果您只想将PHP脚本的结果输出(HTML等)包含到perl生成的页面中,您可以使用反引号通过php-cli调用PHP脚本,如下所示:

First, the test.pl script: 首先,test.pl脚本:

[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: 接下来,test.php脚本:

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

echo "PHP generated this";

?>

Here's the output of running "test.pl": 这是运行“test.pl”的输出:

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

There are the PHP and PHP::Interpreter modules, both have an include method. PHPPHP :: Interpreter模块,都有一个include方法。 Haven't tried either of them myself though. 尽管我自己也没试过。

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. Perl解释器可以执行Perl语法命令,尽管Perl和PHP语法之间存在一些相似之处,但您需要一个PHP解释器来运行PHP代码。 Instead you can use some Perl commands to use output of a PHP script. 相反,您可以使用一些Perl命令来使用PHP脚本的输出。 you should execute the PHP script using the system PHP interpreter in your Perl code, and then use the output of that execution. 您应该在Perl代码中使用系统PHP解释器执行PHP脚本,然后使用该执行的输出。 your PHP code needs to be some command line code that will return (like echo or print) the output. 您的PHP代码需要是一些命令行代码,它将返回(如echo或print)输出。 if you need to pass arguments to the PHP code, use command line arguments when executing the PHP script in your Perl code. 如果需要将参数传递给PHP代码,请在Perl代码中执行PHP脚本时使用命令行参数。 so your Perl code will be like this: 所以你的Perl代码将是这样的:

 // 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脚本可以接收如下参数:

<?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. 我不是Perl程序员,因此可能有其他(更好的)方法在Perl中执行外部程序。 but the general steps are like this. 但一般步骤都是这样的。

You could check out this project: 你可以查看这个项目:

http://metacpan.org/pod/PHP::Include http://metacpan.org/pod/PHP::Include

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. HP :: Include构建在Filter :: Simple和Parse :: RecDescent的肩上,提供了一个Perl实用程序,用于从Perl程序中包含非常简单的PHP文件。

When working with Perl and PHP it is often convenient to be able to share configuration data between programs written in both languages. 使用Perl和PHP时,通常可以方便地在用两种语言编写的程序之间共享配置数据。 One solution to this would be to use a language independent configuration file (did I hear someone say XML?). 对此的一个解决方案是使用独立于语言的配置文件(我听说有人说XML吗?)。 Another solution is to use Perl's flexibility to read PHP and rewrite it as Perl. 另一个解决方案是使用Perl的灵活性来读取PHP并将其重写为Perl。 PHP::Include does the latter with the help of Filter::Simple and Parse::RecDescent to rewrite very simple PHP as Perl. PHP :: Include在Filter :: Simple和Parse :: RecDescent的帮助下完成后者,将非常简单的PHP重写为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. Filter :: Simple用于启用宏(目前只有一个),这会导致PHP插入到Perl源代码中,然后使用Parse :: RecDescent语法对其进行解析以生成适当的Perl。

PHP::Include was designed to allow the more adventurous to add grammars that extend the complexity of PHP that may be included. PHP :: Include旨在允许更冒险的人添加语法,从而扩展可能包含的PHP的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM