views:

291

answers:

5

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?

A: 
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.

mobrule
Where does the output end up, though? The test.pl is designed to output to the command line, not use the mod_perl $r->print...
Kev
output goes to STDOUT filehandle, which I think does the same thing as `$r->print` (see http://modperlbook.org/html/6-4-6-print.html)
mobrule
Hmm...I thought that needed use CGI; or something. Giving it a shot...
Kev
Yeah, the output doesn't seem to appear in the browser as the script runs (it's a long-running script.)
Kev
If refactoring the perl script isn't an option, then why not have the modperl handler kick off the script, which outputs to another file which would be viewable in the browser. Having a long running script be tied directly to an http request brings up all sorts of red flags and alarm bells. What if you accidentally hit f5, or your browser crashes? Wouldn't you want to be able to pick up where you left off?
jsoverson
If you have long running processing, a reverse proxy is nicer way to handle it. The frontend processes handle the little stuff and pass the big jobs to the backend.
brian d foy
A: 

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.

DVK
I know that's one option, but I was more asking whether there's a way to do it without loading another perl executable (and having to track the path in yet one more place)--see OP.
Kev
Added second option on how to do that.
DVK
Might be what I'm looking for, I'll give it a shot.
Kev
Er, maybe not after all. I thought it was just a Perl module, not something I need access to the Apache configs for.
Kev
A: 

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.

Sinan Ünür
This script isn't mainline, I don't need the mod_perl speed here, it just happens to be the environment.
Kev
A: 

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)?

Joe Casadonte
Sounded like a good idea, but when I tried (with a redirect) this is what it said:Can't dup STDERR: Permission denied at C:/Perl/site/lib/Test/Builder.pm line 1826.
Kev
And without a redirect, "No such file or directory" (error code 3328).
Kev
+1  A: 

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.

brian d foy