tags:

views:

364

answers:

4

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?

+2  A: 

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.

Daff
Not a solution I'd recommend at all.
jamiei
Yep it isn't. But that's what he wanted to do.
Daff
may i know why its not recommended
dexter
Among the other reasons, you are reading from the DB in Perl, formatting this as text to output it, slurping it in in PHP and decoding from text to data again. Very inefficient. And you launch a Perl interpreter as a separate process every time, which of course has some overhead. And if something goes wrong you have no reliable mechanism to get back what the error was, unless you code it all yourself. When you have finished doing that you will have reinvented several kinds of wheel, in a inefficient and error prone way.
p.marino
@Daff when i run your code it shows nothing whatever written in `print $cgi->header, <<html;`...html should get displayed in index.php
dexter
You should have a look at the PHP exec command. Maybe you don't even have the rights to execute external programs. Additionally I strongly recommend to consider the repeatedly given advice not to do it this way.
Daff
@Daff i have no rights(or access permissions) issues and i really have to do it this way only ., anyways what <?php echo exec('perl dbcon.pl'); ?> exactly does because it doesn't work in above scenario. have you considered dbcon.pl
dexter
Dexter, you don't seem to know much about PHP and Perl if you have problems with this - so the chances of you geeting this working "by accident" are pretty close to nil. Anyway, you are on windows so maybe you should try "<?php echo exec('C:\PERL\perl.exe D:\whatever\dbcon.pl'); ?>" And yes, "Whatever" is not really the right path, I hope you know your way around your own PC at least...
p.marino
@p.marino ,thanks for the reply, i have question though, what does<?php echo exec('C:\PERL\perl.exe D:\whatever\dbcon.pl'); ?>" have two paths C: and D: what does it do?[i don't have perl.exe i run .php files on browser on localhost] please replay
dexter
p.s all my php and perl files are in C:\xampp\htdocs\com folder
dexter
this was an example to show you that if you are on windows you should include full path to the Perl Interpreter and full path to the .pl file you want to run. If you don't have perl.exe installed how the hell do you suppose to run Perl from inside PHP? Or in any other way?
p.marino
@p.marino ok i understand it now, thanks for reply
dexter
+5  A: 

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.

p.marino
thanks for reply , i know how access DB form both PHP and PERL , my main concern is to call (or execute) perl file from php cause i thought of doing all back-end operations in perl and UI would be in php. thanks again
dexter
No there is no separation. Your perl script still outputs UI elements. You should really have a look at all the PHP frameworks for the separation you want to do.
Daff
"I thought of doing all back-end operations in Perl" is a bit weak unless you want to leverage something that is not available in PHP. If this is the case, go for the "Server" solution. Using Exec() will be horribly slow, inefficient, and brittle.
p.marino
Actually you can use php in a templating system with perl. Added an answer reflecting a solution for this if using the catalyst framework. I think using TT, HTML::Zoom, Mason etc is a better choice... but you can do it.
xenoterracide
+2  A: 

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.

Alexandr Ciornii
A: 

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

xenoterracide