There really is no good way to find out if your script was launched by the web server or from the command line. Any of the environment variables can be set in both situations. I often run CGI programs directly from the command line to test them, for example.
Knowing that if you want to select one environment variable to use, it just needs to be one that you do not specify in another situation, or one that you set in both, but give different values. In this case, select any environment variable that you like.
If you want more features, you can use something like IO :: Interactive to determine if you are connected to the terminal. If you havenβt done so, the filehanandle that returns is_interactive is the null file descriptor, and the output does not go anywhere:
print { is_interactive() } $http_header;
If you don't like how IO :: Interactive decides, you can override is_interactive . This is a very short piece of code, and the higher-level interface is very nice.
brian d foy
source share