how to include .pl (PERL) file in PHP

2019-10-02 08:45发布

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

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

i have tried in index.pl

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

and dbcon.pl has

#!/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
}

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

how to overcome this problem?

note: i am running this in windows environment

is there any other way to do this?

标签: php perl include
4条回答
做自己的国王
2楼-- · 2019-10-02 09:05

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

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

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

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

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.

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

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).

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.

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

查看更多
太酷不给撩
3楼-- · 2019-10-02 09:16

There is Perl PECL package to integrate Perl into PHP.

P.S. IMHO it is better to use templating system like Template Toolkit in Perl. You can even use Perl inside templates.

查看更多
SAY GOODBYE
4楼-- · 2019-10-02 09:26

If you're using Catalyst you could us Catalyst::View::PHP I suspect it will give you more clues on how to use php as your templating system. It also mentions PHP::Interpreter

查看更多
相关推荐>>
5楼-- · 2019-10-02 09:29

What you are trying is not possible that easy. You will have to execute the perl script with PHP, capture the output and print it like:

<?php echo exec('perl dbcon.pl'); ?>

As mentioned that is not a good thing to do. For a good separation between backend and user interface you should have a look at existing PHP frameworks.

查看更多
登录 后发表回答