0

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?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Kev
  • 15,899
  • 15
  • 79
  • 112
  • `perl` is not in the `%PATH%` for the account under which `httpd` is running. – Sinan Ünür Oct 07 '09 at 21:19
  • I mentioned that might be the case in the OP – Kev Oct 07 '09 at 21:24
  • I just checked Control Panel->System->Advanced->Environment Variables and it's in the system path...so I don't know why that wouldn't end up in the path of all accounts. – Kev Oct 08 '09 at 13:52

5 Answers5

1

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
  • 129,424
  • 31
  • 207
  • 592
0
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.

mob
  • 117,087
  • 18
  • 149
  • 283
  • 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 Oct 07 '09 at 20:45
  • 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) – mob Oct 07 '09 at 20:58
  • Yeah, the output doesn't seem to appear in the browser as the script runs (it's a long-running script.) – Kev Oct 07 '09 at 21:12
  • 1
    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 Oct 07 '09 at 23:54
  • 1
    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 Oct 08 '09 at 23:33
0

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
DVK
  • 126,886
  • 32
  • 213
  • 327
  • 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 Oct 07 '09 at 20:53
  • Added second option on how to do that. – DVK Oct 07 '09 at 21:04
  • Might be what I'm looking for, I'll give it a shot. – Kev Oct 07 '09 at 21:14
  • Er, maybe not after all. I thought it was just a Perl module, not something I need access to the Apache configs for. – Kev Oct 07 '09 at 21:30
0

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
  • 116,958
  • 15
  • 196
  • 339
  • This script isn't mainline, I don't need the mod_perl speed here, it just happens to be the environment. – Kev Oct 07 '09 at 21:23
-1

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
  • 15,888
  • 11
  • 45
  • 57
  • 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 Oct 08 '09 at 13:15
  • And without a redirect, "No such file or directory" (error code 3328). – Kev Oct 08 '09 at 13:39