Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell if STDOUT of Perl Script is redirected

So I wrote a simple html 1.0 server, and I have some perl scripts on the server. In particular, I have this script called my histogram, that is an html form with a form action equal another cgi file. Here's the code:

print "<form action=\"tallyandprint.cgi\" method=\"GET\">";

Now, when I call tallyandprint.cgi, it plots a graph with gnuplot and sends it to the user's browser (STDOUT is redirected in the html server code, so perl inherets it). Now, I also want to be able to run tallyandprint.cgi from bash, but take a different style of arguments. Right now, I use perl parsing to grab the patterns by parsing the url, and separating the contents between the + symbol (example:?pattern=1+2+3+4 is what the url is).

Thats fine and dandy, but I don't want my arguments to be written in bash as 1+2+3+4, but rather separated differently. I tried to use perl's version of isatty(), but since the input is always from the terminal (because the server executes it), I cannot distinguish between whether the input is from bash or from web browser this way.

My next though was to find out if STDOUT is redirected. Since if the webserver runs the cgi, the STDOUT will be redirected to the socket that the user is connected to. If run in bash, the STDOUT should be the normal tty. How can I check this in perl?

like image 896
Magn3s1um Avatar asked May 06 '12 20:05

Magn3s1um


1 Answers

if (-t STDOUT) {
    say "STDOUT is probably not redirected";
}
else {
    say "STDOUT is probably redirected";
}
like image 82
salva Avatar answered Oct 08 '22 01:10

salva