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

Hi Guys, I have a Perl script running as a Daemon, It basically listens on a TCP Prot and writes all the data recieved to a txt file.
once this file is created i have a cron script that runs every min to check for files and process them using PHP
My question is..
Is it possible to add some code at the end of the Perl script to call a PHP script?
Any help will be great
Kenny

Replies are listed 'Best First'.
Re: Run another script
by Corion (Patriarch) on Oct 19, 2007 at 07:48 UTC

    There are many options:

    • exec(), if you want to start the other process and immediately exit your program
    • system(), if you want to start the process and wait until it has finished to do some post processing
    • backticks or qx if you want to run the other program and process its output
    • IPC::Run or IPC::Open3 if you want even more interaction with the other program
      More options:
      • Use the PHP module's embedded interpreter to run the PHP within your Perl script (may or may not work; I just know this exists, I've never used it myself)
      • Given the similarities of PHP and Perl syntax, it may be simple to convert the PHP file to Perl and do all of the work in a single program. If the only purpose of the txt files is to communicate data to PHP, this could eliminate the need to create them at all.
Re: Run another script
by perlfan (Parson) on Oct 19, 2007 at 13:33 UTC
    You should have the Perl daemon write the files to some sort of spool directory (don't forget, write in a tmp location, then mv it to the spool directory so it is atomic).

    You should have another daemon watch this directory, then react accordingly when a file is detected. Ideally, this daemon should be written as a shell script. When a file is detected, you can handle it however you wish - this includes calling a PHP script...or doing anything else that is possible in a shell script.

    I've actually written such a monitor daemon, and if you /msg me I'll send you the link to it. I think it'll suit your purpose well.

    It is paired with a Perl utility that I wrote to extract data from "message files", which really just consists of ARG="value .." pairs.
Re: Run another script
by pajout (Curate) on Oct 19, 2007 at 16:15 UTC
    If your PHP script sits under Apache, you can launch it by requesting your http://localhost/myspecialurl.
    See HTTP::Request manual:
    require HTTP::Request; $request = HTTP::Request->new(GET => 'http://www.example.com/'); $ua = LWP::UserAgent->new; $response = $ua->request($request);