Sending the output of a command-line perl script to the browser via mod_perl

1.4k Views Asked by At

I have a plain perl script that can be run from the command-line via perl -w test.pl. I then have a mod_perl2 script that can be accessed from a web browser. I want to have the latter call the former and send the output to the browser, flushing as it goes.

The mp2 script doesn't have a shebang line, because it's mod_perl, so it doesn't know where perl lives. Also, calling system('perl -w c:\\path\\to\\test.pl') results in the error:

    'perl' is not recognized as an internal or external command,
    operable program or batch file.

for some reason I can't figure out, since it's in my path variable. Maybe not for the account Apache is running under.

Is there some way to run the script and capture its output without calling the perl executable via system()? I.e., something that uses the interpreter that's already loaded?

5

There are 5 best solutions below

1
On BEST ANSWER

Clearly, the problem is that the %PATH% of the account under which httpd is running does not include the path to perl. You can set that up in httpd.conf using PerlSetEnv.

5
On
do "/path/to/test.pl";

or

require "/path/to/test.pl";

will load and evaluate the contents of a file.

One caveat about require is that evaluating the file has to return "true". The usual way to do this is to put a "1;" at the end of your script.

4
On

You need to find where Perl binary lives (on Unix, do which perl, on Windows, find Perl icond and find command line path or find the directory where perl is installed - for example, "c:\program files\myPerlDistro\bin\perl.exe"

Then you need to either add that full path explicitly to your qx// (don't use system as it loses output) call, or add that path into Apache's PATH variable.

A second option is to use EmbPerl - it has Execute directive that in-place-executes other scripts and includes their output - in the same interpreter. It runs under mod_perl.

0
On

Aside from the mod_perl issue, the location of the current perl interpreter is in $^X. If you aren't running under mod_perl, that's how you should find perl. Inside mod_perl, of course, you probably don't want that one since it's baked into apache.

Some people have mentioned %PATH%, but I recommend against that. Just find out the full path to Perl and use it explicitly without relying on the %PATH%. Whether you hard-code that or set it in a config is up to you.

2
On

If this is Win32 (as your tag indicates) can't you just associate the .pl extension with Perl via standard Windows stuff (e.g. hack the registry or go under Tools > Folder Options > File Types in an Explorer window)?