petdance has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to do some Perl code that can access PHP code, so that we don't have to rewrite the working PHP code in Perl. It looks like we can pass stuff around via the Apache notes table. Here's what I've done as proof of concept:
package TW::Apache::Play; use strict; use warnings; use Apache::Request; use Apache::Constants qw( :common ); sub handler { my $r = Apache::Request->new( shift ); my $username = $r->param('username') || "alester"; $r->send_http_header( "text/html" ); print "<html><body>"; print "<h3>Looking up \"$username\" via PHP</h3>"; my $sub = $r->lookup_uri( "/foo.php" ); $sub->notes->set( username => $username ); my $rc = $sub->run(); my $notes = $sub->notes; print "custid=$notes->{custid}<BR>"; print "contact=$notes->{contact}<BR>"; print "All done!"; print "</body></html>"; return OK; } 1;
This mod_perl handler fetches a parm from the URL, and passes it into the notes table for a subrequest. Then, it executes that subrequest, which is the following PHP file:
<?php $username = apache_note( "username" ); $cols = sqldo( "select custid,contact from users where username=:u +", Array( ":u"=>$username ) ); apache_note( "custid", $cols["CUSTID"] ); apache_note( "contact", $cols["CONTACT"] ); ?>
The PHP gets the username note from Apache, does a lookup in the database, and then passes back two columns in the notes table. Control is the passed back to the mod_perl handler, which extracts those two columns from the notes table and displays them on the screen.

Of course, in real life, the PHP code will be calls to some hairy business logic that is thoroughly debugged that I don't want to rewrite on the Perl side.

Is there any danger in doing this? As I understand the notes table, they're per-request only, and won't stick around after the request is completed, so I don't have to worry about children polluting each other. Surely there's a performance hit of some kind, but I assume it's only relatively small.

Any words of experience from anyone out there?

xoxo,
Andy

Replies are listed 'Best First'.
Re: Talking to PHP from Perl
by Somni (Friar) on May 29, 2004 at 02:24 UTC

    There is no danger in this, and yes, notes are per-request. This is a pretty common method of passing data between Apache extensions. The performance hit should indeed be negligible.

    I'm not sure what other advice to give you; this is a good way to use notes, and a good way to pass data between sub-requests and phases of a request. I can't say I particularly like the concept of running a PHP program as an API to a lot of business logic, but given your description it doesn't look like you can get around that easily.

Re: Talking to PHP from Perl
by cees (Curate) on May 29, 2004 at 04:12 UTC

    From your code it looks like this is written to work in mod_perl1. I don't see any problems with your approach (it's quite nifty), but I thought I would throw out a second way of handling this type of problem with the new filter support in apache2 and mod_perl2.

    Both PHP and mod_perl can act as a filter. In your case, you should be able to leave the PHP page unaltered, and filter it's output through a custom mod_perl filter. The filter gets as input the generated PHP page, and can parse out any info it needs and spit out a completely different page.

    But since you have what looks to be a working system, it probably makes sense to stick with that.

    - Cees

      We'll put together a dummy page that generates no output, but just sets note values. We've got a fair amount of business logic in PHP classes that we just don't have the time to port over to Perl.

      If we didn't have this bridge, we'd have to do the entire project in PHP, instead of being able to chip away at it and do some Perl for it.

      xoxo,
      Andy