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

I was wondering how one can tell if a script is being run online or offline. I see this is done in CGI.pm and would like to use it to avoid %ENV dependant code malfunctions. Thanx a bundle,

- p u n k k i d
"Reality is merely an illusion, albeit a very persistent one." -Albert Einstein

Replies are listed 'Best First'.
Re: Script running online?
by chromatic (Archbishop) on Mar 26, 2001 at 09:20 UTC
    You could check for the existence of an environmental variable set by web servers, such as $ENV{SCRIPT_NAME}. This may or may not work well with persistent technologies such as mod_perl, but scripts running there don't seem too likely to run in "offline" mode.
Re: Script running online?
by busunsl (Vicar) on Mar 26, 2001 at 10:59 UTC
    You can try this:
    if (-t STDIN) { print 'running in terminal'; } else { print 'running in batch'; }
    The '-t' filetestoperator checks if the filehandle is a tty.
Re: Script running online?
by sutch (Curate) on Mar 26, 2001 at 20:32 UTC
    PotPieMan's solution is close to what I came up with. I tried:

    print getpwuid($<);
    Run from the command line it printed:

    sutch

    which is my username. Run within a CGI script it printed:

    apache

    which is the user that HTTPD runs under on my machine.

Re: Script running online?
by PotPieMan (Hermit) on Mar 26, 2001 at 10:14 UTC
    It may not be absolute, but I guess you could check $ENV{'USER'}. I do most of my development and testing as one user, whereas I run my scripts as my Apache user. Maybe?

    Update: Sorry, this won't work. I should have checked before posting, but $ENV{'USER'} is not set by the Web server for security reasons. So, chromatic's response is probably the way to go. Sorry for the confusion.

    --PotPieMan