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

I typically look for $ENV{'SERVER_NAME'} or other items that are set automatically by the webserver, with the assumption that most people's shell won't have it set. Another good one to check might be $ENV{'GATEWAY_INTERFACE'}.

  • Comment on Re^2: Telling if your script is running from web page or CLI

Replies are listed 'Best First'.
Re^3: Telling if your script is running from web page or CLI
by CountZero (Bishop) on Sep 29, 2005 at 19:46 UTC
    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

      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