title: Perl how to detect if running from the command line or called from browser
body: <p>Is there a good way to detect if the Perl script is called from terminal/DOS or called from a web server.</p>
  
  <p>I currently have this code:</p>
  
  <pre><code>sub cli {
      # return 1 if called from browser, 0 if called from command line
      if  (defined $ENV{GATEWAY_INTERFACE} ||  exists $ENV{HTTP_HOST}){
          return 0;
      }
      return 1;
  }
  </code></pre>
  
  <p>but read that these ENV variables are not set by all servers.</p>
  
  <p>The reason for this if the script run from terminal I will print text/plain formatted messages, if run from browser I will print HTML formatted messages.</p>