With eval, you can just load a perl program and run it. Whatever the loaded code does, it does, as if the loaded code was originally in your CGI program. The program could print to STDOUT, or save its results in a variable for further processing, or whatever you want. The applicability of this depends on the nature of the perl code you will be loading. If it is code written by you that is aware that you will be loading and executing it, that's great. But if it's a stand-alone program not expecting to be eval'd, you have to watch out for it using or changing global variables, it containing USE statements which are not meant to be executed except at the top of the program, etc. Eval lets you do some clever things in a closed universe of your own, but is not the most general solution.
Without knowing exactly what you are tyring to do, it is hard to know if this is what you want. I'd guess not, however. In the more common case where you have a program that you or someone else wrote independently, and you just want to run it and get its output, you could use backticks. Backticking runs a separate program and returns the program's output in a variable which you can then print to STDOUT or whatever you want. As always, take lots of care when using backticks or system or similar things in your program; if a bad guy can give you input that changes the path to the program you want to run, he can do enormous damage. Or, you could read a program's output by using Open and pipes. For example (untested): open(RESULT,"perl your-program.pl |") or die "whoops"; print $_ while(<RESULT>); # or whatever... close RESULT: I'm sure there are other approaches. There are subleties to all of these, so it might be useful to know more about the specifics of what you are doing; it might affect security, blocking behavior, etc. To have STDERR of the external program print to STDOUT, you could do something like: my $result = `your-program 2>&1`; # redirect STDERR (2) to STDOUT (1) You could also use this redirection with an Open call. Chapter 16 of the Perl Cookbook goes into all of this at some length. ----- Original Message ----- From: "Paul Lussier" <[EMAIL PROTECTED]> To: "Mark Aisenberg" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Friday, October 05, 2001 3:48 PM Subject: Re: [Boston.pm] Safe execution of 1 perl prog from another? > > In a message dated: Fri, 05 Oct 2001 15:29:52 EDT > "Mark Aisenberg" said: > > >If the command line perl program prints to STDOUT, you > >could load and 'eval' it. This will store the results in a > >variable, which your CGI program could print to the > >browser. > > I thought about eval. Maybe I'm being overly dense on this > particular Friday afternoon, but I couldn't quite figure out how > exactly to do this? > > Can someone beat me with an appropriately sized cluestick? > > Thanks > > -- > > Seeya, > Paul > ---- > > God Bless America! > > ...we don't need to be perfect to be the best around, > and we never stop trying to be better. > Tom Clancy, The Bear and The Dragon > > >
