This post originated from an RSS feed registered with Java Buzz
by dion.
Original Post: Write PHP in Perl with PHP::Interpreter
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
And finally—yes, you heard right—Bricolage now supports PHP 5 templating in addition to the existing Perl-based templating architectures (Mason, Template Toolkit, and HTML::Template). So how did we add PHP 5 templating to a mod_perl application? Easy: we hired George Schlossnagle of Omni TI to write PHP::Interpreger, an embedded PHP 5 interpreter. Now anyone can natively execute PHP 5 code from a Perl application. Not only that, but the PHP 5 code can reach back into the Perl interpreter to use Perl modules and objects!
So picture using this Perl in this PHP:
<?php
$perl = Perl::getInstance();
$perl->eval("use DBI");
$perl->eval("use DateTime");
$dbh = $perl->call("DBI::connect", "DBI", "dbi:SQLite:dbname=dbfile");
$dbh->do("CREATE TABLE foo (bar TEXT, time DATETIME)");
$now = $perl->call("DateTime::now", "DateTime");
$ins = $dbh->prepare("INSERT INTO foo VALUES (?, ?)");
$ins->execute("This is a test", $now);
$sel = $dbh->prepare("SELECT bar, time FROM foo");
$sel->execute();
$a = array("foo", "bar");
foreach ($sel->fetch() as $val) {
echo "$val\n";
}
$sel->finish();
$dbh->do("DROP TABLE foo");
$dbh->disconnect();
?>
and what about PHP in Perl (Mason)?
<%perl>;
for my $e ($element->get_elements(qw(header para _pull_quote_))) {
my $kn = $e->get_key_name;
if ($kn eq "para") {
$m->print("<p>", $e->get_data, "</p>\n");
} elsif ($kn eq "header") {
# Test sdisplay_element() on a field.
$m->print("<h3>", $burner->sdisplay_element($e), "</h3>\n");
} elsif ($kn eq "_pull_quote_" && $e->get_object_order > 1) {
# Test sdisplay_element() on a container.
$m->print($burner->sdisplay_element($e));
} else {
# Test display_element().
$burner->display_element($e);
}
}
$burner->display_pages("_page_");
</%perl>