in reply to Re^2: Telling if your script is running from web page or CLI
in thread Telling if your script is running from web page or CLI

That is not always going to work. If I remember correctly if you run under mod_perl, you start with an extremely limited environment, such as:
MOD_PERL = mod_perl/2.0.1 MOD_PERL_API_VERSION = 2 PATH = X:\webserver\perl\bin\;C:\WIN ...
and that's it.

Checking the environment is probably not very portable either.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re^4: Telling if your script is running from web page or CLI
by jhourcle (Prior) on Sep 30, 2005 at 00:20 UTC

    The question was specifically about CGI. As part of the CGI specification, you'll see that some environmental variables are specifically set for every request. (including the two I mentioned originally)

    It's always possible that some of the other more-than-just-CGI implementations (FastCGI, mod_perl, etc), behave differently, as well as software that doesn't correctly implement the CGI specifications.

    However, for my install of mod_perl (using Apache::Registry, Apache 1.3, Mac OS X), in using the following simple script:

    #!/usr/bin/perl -- use Data::Dumper; print "Content-Type: text/plain\n\n", Dumper \%ENV;

    I get after censoring out the host name:

    You'll notice that 'GATEWAY_INTERFACE' is still defined, and indicates that it's not a standard CGI.

    Using Apache::PerlRun is the almost identical:

      The question was specifically about CGI.
      Read again the beginning of the original question:
      I'd like to be able to call a script via a browser or via the CLI/shell and have that script 'know' what’s calling it
      It's not only about CGI, it's about all kinds of dynamic webpages fueled by Perl scripts.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law