开发者

how to include .pl (PERL) file in PHP

开发者 https://www.devze.com 2022-12-27 07:46 出处:网络
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 operationswould be done inPerl file.

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 wa开发者_如何学Gornings;
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?


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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消