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

It is possible to have a CGI program have contents in @ARGV. One way is too look for specific keys in (or NOT in) %ENV.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
  • Comment on Re: Telling if your script is running from web page or CLI

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

    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'}.

      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: