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

This is probably my stupidest question yet, but I feel I must ask anyway. I've worked mostly on CGI scripts in the past and had a good understanding of the server environment I was working in. However, now I'm working for a new company on editing this perl script, its not CGI, and doesn't use any libraries. Basically I'm just trying to get it to print out the environment variables by doing:

foreach (keys %ENV) { print OUTFILE "$_ => $ENV{$_} \n"; }

I've seen this done on this site, and from experience I know this works, but I get a blank page when this script runs. I don't have access to error logs like I normally do. And to run the scripts I have a batch script that kicks off the perl script on the server, I don't see any errors there either. So is there a reason this would not print out anything? Or is there another way to get the environment variables?

Replies are listed 'Best First'.
Re: Figuring out the environment
by Zaxo (Archbishop) on Sep 25, 2002 at 14:48 UTC

    The syntax of that statement is fine and it should do what you want. That suggests that OUTFILE is not open to write. Have you tested this on the command line?

    Without a shebang line, the batch script must be calling this as '/usr/bin/perl [flags] LIST'. Add -w to the flags and lead off your scripts with use strict;. That will get you error messages. $^W can be set if you can't get warnings enabled in the batch script.

    After Compline,
    Zaxo

      Ok correction, I was able to get a list of environment variables, there's a copy lag from one server to the main webserver so unaware of this I thought it didn't work. However the list of variables does not contain one for current directory or PWD.

      $cwd = $ENV{'PWD'};

      I tried this code with no luck, this time I made sure the page updated :) But more importantly why wouldn't I have this environment variable, and is there another way to get the current directory?

        Do you know what OS the server is running? AFAIK, Microsoft OS's don't have an environment variable for the current directory. However, the Cwd module, which is one of the Perl core modules, provides a function called cwd which returns the current working directory.
        use Cwd; $cwd = cwd(); print "My directory is $cwd\n";

        kelan


        Yak it up with Fullscreen ChatBox

        IMO, CWD is the most portable solution for finding the current working directory.
Re: Figuring out the environment
by fglock (Vicar) on Sep 25, 2002 at 14:31 UTC

    I'd start with:

    foreach (keys %ENV) { print "$_ => $ENV{$_} \n"; }

    Maybe your batch file is piping STDOUT instead of using OUTFILE

      Thanks fglock, I'm actually able to print out to the webpage. Like this line prints out a link:

      print OUTFILE "<a href=$tlink>$field2</a>\n";

      but for my other code it don't print out anything.

Re: Figuring out the environment
by Willman023 (Scribe) on Sep 25, 2002 at 14:33 UTC
    Oh I forgot to mention that there is no shebang line.

    Does that mean it's just running the script like a comand line batch file?