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

Oh wisemen of Perldom

I have a script that uses backticks to run a program. It works
fine when I run the script from the command prompt, however,
when the script is run from a web server it fails.

I believe the reason is that the environment paths are not set for
the web server so my backticked program doesn't run right. But I do
have another script (setpaths)that will setup the enviroment paths.

I want to do this correctly and I don't really know how unix and shells
work. I want to call the 'setpaths' from my script, but I from what I hear
This will create a shell, set the paths, then close the shell and in the end
nothing got done.

Any suggestions???

Here's an example of my code
`/opt/bin/myprog test`; if ($? != 0){ # if successful returns a 0 print "<br>Error: $?<BR>\n"; print "<br><Blink>ERROR!!!</blink><B>Program did not run correctly< +/b><BR>"; }

How can I stick another line in here that will run my setpaths script and
keep the paths for myprog ??

Replies are listed 'Best First'.
Re: Environment Paths
by Fastolfe (Vicar) on Nov 20, 2000 at 23:46 UTC
    Your best bet is to set the environment variables from within Perl first, then execute your other programs.
    $ENV{PATH} = "/bin:/usr/bin:/some/weird/bin"; $ENV{SOME_VAR} = "some value"; $output = `some_command that I want output for`; system("some_command with output to STDOUT");
    Of course, if it's because of your PATH variable that you're having problems, you're far more secure doing something like this:
    system("/absolute/path/to/some_command", "argument1", "2", $etc);
Re: Environment Paths
by AgentM (Curate) on Nov 21, 2000 at 00:41 UTC
    Careful! Leaving something as obvious as $ENV{'PATH'} unset where your program relies on it is a blatant security hole. Anyone could set this beforehand to execute arbitrary code from a setuid root script! The best solution is to hard code the path right in to prevent any further complication. '/usr/bin/cat' instead of 'cat', for example. I hope you are also enabling taint checking for this CGI script! If you use any form-inputted data to run your program, the program will fail, as it should since an executed program could have some lethal or illegal or hacker arguments which perl would not be able to catch. The best solution is to use a CPAN module, but if that doesn't exist, then running executables from a CGI is generally very dangerous- every CGI execution will now consist of 2 processes- twice as many (not considering any prebuffering or other CGI speederuppers). It's best to avoid it, but if you can't, then closely monitor the the path to the binary as well as any arguments it may take.
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
Re: Environment Paths
by wardk (Deacon) on Nov 20, 2000 at 23:35 UTC

    something like this?

    use Env qw(TNS_ADMIN); $TNS_ADMIN = "/opt/oracle/local/config";
    try:
    perldoc Env
Re: Environment Paths
by elwarren (Priest) on Nov 21, 2000 at 04:05 UTC
    I think you should just be able to add a require line at the top of your script and it should load your file at runtime. RTFM for the details.
      Thanks...I'll look into this.