如何在PHP中包含.pl(PERL)文件 [英] how to include .pl (PERL) file in PHP

查看:194
本文介绍了如何在PHP中包含.pl(PERL)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php(index.php)中有两个页面,在Perl中有另一个页面(dbcon.pl)。

i have two pages one in php(index.php) and another one in Perl(dbcon.pl).

基本上我希望我的php文件只显示UI和所有数据操作都将在Perl文件中完成。

basically i want my php file to show only the UI and all the data operations would be done in Perl file.

我在index.pl中尝试过

i have tried in index.pl

<?php include("dbcon.pl");?>
<html>
<br/>PHP</br>
</html>

和dbcon.pl有

#!/usr/bin/perl
use strict;
use warnings;
use DBI;

use CGI::Simple;
my $cgi = CGI::Simple->new;
my $dsn = sprintf('DBI:mysql:database=%s;host=%s','dbname','localhost');

my $dbh = DBI->connect($dsn,root =>'',{AutoCommit => 0,RaisError=> 0});


my $sql= "SELECT * FROM products";
my $sth =$dbh->prepare($sql);
$sth->execute   or die "SQL Error: $DBI::errstr\n";
while (my @row = $sth->fetchrow_array){
print $cgi->header, <<html;
<div>&nbsp;@row[0]&nbsp;@row[1]&nbsp;@row[2]&nbsp;@row[3]&nbsp;@row[4]</div>
html
}

但是当我在浏览器中运行index.php时会打印出来dbcon.pl文件中的所有代码而不是执行它

but when i run index.php in browser it prints all the code in dbcon.pl file instead of executing it

如何解决这个问题?

注意:我在Windows环境中运行它

note: i am running this in windows environment

有没有其他方法可以做到这一点?

is there any other way to do this?

推荐答案

请问问题到底是什么?我没有在Perl代码中看到任何特殊,所以你要么:

May I ask what the problem really is? I don't see anything "special" in the Perl code, so you either:

a)不知道如何从PHP访问你的数据库(即你不要不知道PHP)或

a) Don't know how to access your DB from PHP (i.e. you don't know PHP) or

b)不知道Perl正在做什么(即你不知道Perl)或

b) Don't know what Perl is doing (i.e. you don't know Perl) or

c)可能你的环境已经设置好了,你可以使用Perl DBI,但不能用PHP做同样的事情。

c) possibly your environment is set up so that you can use Perl DBI but you can't do the same from PHP.

这个 链接 应该指导你做你在Perl做的事情直接来自PHP。您可以轻松找到各种PHP / DB组合的几十个示例。

This link should give you pointers to do what you are doing in Perl directly from PHP. You will easily find dozens of examples for various PHP/DB combinations.

唯一的另一种方法是执行另一个海报建议:调用Perl脚本并解析结果(按标准打印)。

The only other way would be to do what another poster suggests: invoke the Perl script and parse the result (printed to standard out).

这是rubygoldbergsque,脆弱且不可接受作为解决方案,除非您绝对不顾一切地使用仅作为Perl模块提供的东西(示例中不是这种情况)你发布了。)

This is rubygoldbergsque, brittle and unacceptable as a solution unless you are absolutely desperate to use something that is available only as a Perl module (which is not the case from the example you posted).

一般来说,如果你想用一种语言完成某些事情并用其他语言来表达,那么最好的方法就是(在你的情况下) Perl作为一种服务器运行,即一个单独的进程 - 并使其使用XML-RPC或其他一些轻量级协议公开服务。

In general if you want to have something done in a language and use it from some other language the best way would be to make the (in your case) Perl run as a sort of "server", i.e. a seperate process - and make it expose services using XML-RPC or some other lightweight protocol.

使用exec调用程序()或类似的结构是非常糟糕的做法。

INVOKING PROGRAMS WITH exec() OR SIMILAR CONSTRUCTS IS EXTREMELY BAD PRACTICE.

这篇关于如何在PHP中包含.pl(PERL)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆