Aaron Gadberry

Help – v. helped, help·ing, helps

Was this site helpful?
My Amazon.com Wishlist

How To: Run a Perl script from PHP

23rd January 2006 - By Aaron

While you may be dreading this part of your project, this task is surprisingly easy for non-major implementations.

The example I’m going to use for this is implementing a perl calendar into a php web page.

First off, you need to have your web server correctly serving the perl as is. In other words, call the perl you will want from a web browser. If http://www.yoursite.com/cgi-bin/helloworld.pl works right, then you’re set. If it doesn’t, you need to troubleshoot why your perl isn’t being executed correctly.

Next you need to use a simple include-style command called virtual.

Here’s an example of this in use.

$src = "/cgi-bin/calendar.pl?template=aaron_minical.html&calendar=web"; if ( !@virtual( $src ) ) { print( "There is an error in your perl script. Try executing it directly from the web browser." ); }

The results from that src file will be displayed inline in your php page. In my case, it creates a calendar in the middle of my web page. You also notice that arguments are passed to the perl script in the usual get manor. This works because virtual goes a level deeper than include or require. Virtual makes the request from apache, instead of handling it internally. Apache, in turn, executes the call as a seperate page visit, and returns the results.

You also notice there is not an absolute filename reference, as in /var/www/vhosts/yoursite.com/cgi-bin/calendar.pl. This is because the way it is included. Because Apache handles the request, it’s a web request, not a local file request, so it must be relative the to domain.

5 Responses to “How To: Run a Perl script from PHP”

  1. Michael Workman Says:

    Thanks for clearing this up for me. I have searched far and wide to do this…and you key about the absolute file name reference…brought success. Thanks for sharing

  2. jarnail Says:

    Good one but not helped in my project. I need to implement perldesk tool in php project. But i could not do with this help. If you have any idea that pls response.

  3. subeen Says:

    I am facing difficulties while running a perl program from php script. The php script is hosted in the web server. If I click a button then, the following command is to be executed:
    exec(‘perl ./scripts/prog.pl -n 150′);

    But when I click the button, the php page waits for the perl script to finish execution. Giving ‘ps ax|grep perl’ command, I see the perl script is running:
    23900 ? S 0:00 perl ./scripts/prog.pl -n 500

    But it’s doing nothing…

    Can you guess the possible reason for this behavior?

  4. Mike Says:

    is it possible to use this to “POST” a set of form vars to a page and redirect to that page – basically simulate what a browser does when you click a Submit button on a form?

  5. Preeti Says:

    I was not able to “POST” from my perl script using this method. ANy ideas

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>