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


In reply to Talking to PHP from Perl by petdance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.