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

Dear All,
I am in the concluding stages of writing a program that will allow a user to perform a search. It wont actually work as i need to access external biology databases who cnat as yet grant me access. SO, what im trying to do is knock up a prototype to show the project manager what can be done, creating graphs etc.

The only thing is, i need the code to work using BioPerl, which doesnt have DBD, DBI, GD::Graph, Archive::Zip and a few other things i need.

I have an online DB stored in Biolinux and want to show them that i can upload data to this DB. I know that they can download the necessary modules later but they are not sure about the methods.
But, with no DB handling means i dont think it can be done with perl.

Thus, i want to try and run a PHP script to rip data from a text file and bung it into the DB. I have got a simple thing working but i dont have a clue how to call the PHP script from inside my CGI perl code.

Simply Can i run PHP using my perl cgi script.. run off a button press
Can this be done............
Cheers.

Replies are listed 'Best First'.
Re: Running PHP
by itub (Priest) on Apr 16, 2005 at 18:21 UTC
    You can run PHP from the command line:

    php -q myscript.php

    which means that you could run it from a Perl program:

    my $ouput = `php -q myscript.php`;

    The '-q' argument is for suppressing the HTTP headers. If you need them for some reason, just don't use -q.

      itub, I see what you have done, but not sure how this differs from the CGI redirect I do to run my PHP script from my Perl. Also, it doesn't solve the problem I have passing data (It would be cool if there was an PHP version of HTML::Template).

      I thought I might we able to print the PHP as a here doc, but that bombed as well. Thanks.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        I can think of three ways of passing data to PHP:
        1. Call it as a real CGI, using LWP to connect to your web server
        2. Emulate the CGI environment by setting variables such as $ENV{REQUEST_METHOD} and $ENV{QUERY_STRING}. The problem is that PHP is usually compiled in a way that disallows this for security reasons.
        3. Create the PHP script on the fly, including the data there. A simple example would be this:

          # multiply $x by $y using php use IPC::Open2; my $x = 5; my $y = 7; my $php = "<? echo $x * $y ?>"; my ($r, $w); my $pid = open2($r, $w, 'php -q'); print $w $php; close $w; my $result = <$r>; print "$x * $y = $result\n";
      Excellent!!
      Well done that man (or woman ... covering ones ass).

      Cheers
      MonkPaul.

Re: Running PHP
by bradcathey (Prior) on Apr 16, 2005 at 17:47 UTC

    MonkPaul, I don't think you can. That's a wimpy answer, but I have struggled with this for a while. Here's the deal: I wrote a content manager based in Perl. However, I wanted to use this great wysiwyg text editor written in PHP. It would have been so easy to be able to call the editor from my Perl for the purposes of passing data, but I couldn't figure out a way to do it. I eventually had to learn enough PHP to tap the database for a session id that I write to MySQL on the Perl side so the PHP can then grab all the associated data.

    Going the other way, from the text editor to the Perl was easy, as I could just call the Perl from the form action.

    I thought I had asked about all this here at PMs, but I can't find it. However, at the time I was asking, here or in other forums, the answers came back negative. I wrote the author of the PHP editor and he said there was no way to bridge the gap--amazingly. I thought the fact the Perl and PHP are related would mean a simple connection between two programs written in both.

    I've solved the problem for myself, but will watch this thread to see what others might have to say.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      Cheers mate,
      I thought it might be simple enough...as you said... they are supposed to be related.

      Damn this infernal project
      If you could let me know what you find out then i would be very grateful.

      MonkPaul.

        Where does this notion that Perl and PHP are somehow related come from? (Okay I know that the first versions of PHP were actually written in Perl.)

        /J\

Re: Running PHP
by injunjoel (Priest) on Apr 18, 2005 at 00:01 UTC
    Greetings all,
    Though there are a number of modules available at CPAN relating to PHP, PHP and PHP::Include::Vars to name a few, my question to you is why are you not able to extend the objects offered in BioPerl to work with DBI, GD and Archive::Zip? Since you are using BioPerl in the first place it sounds as though your project manager is not opposed to OpenSource software and you should be able to, in theory as Im only guessing at this point, download and use other free modules like those from CPAN... or am I missing something?
    Looking at the specs for BioPerl 1.4 on BioLinux.org I see that GD is bundled inside and that DBI and DBD::MySQL are suggested, so the developers understood that people would need graphics and DB interaction. It seems like it would be easy to create your own objects that use BioPerl along with your own DB and Archiving subroutines to get the desired functionality that you want.
    Basically what Im trying to say is "don't through the baby out with the bathwater", as the saying goes "There is more than one way to do it" and sometimes those ways are not the most obvious.
    Beside Perl Objects are beautiful things once you start writing them.

    Update!
    Here is an article about how to run Perl through PHP for those interested.

    -InjunJoel

    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
      I understand what your saying about downloading the modules for the BioPerl system, but as this was only a short term project i was after a quick fix solution.

      Im glad you brought it to my attention that the DBI modules are suggested but not included, which may enable me to swing an over-riding vote in favour of my system.

      If anybody else has any ideas please let me know.