Ideally, this would be scriptable in shell, but Perl or Python would be fine. C code could be helpful, but probably fails cost/benefit.
I recognize that redirection to a
(my comment turned to answer per Alex Dupuy's suggestion)
Since you said you can use perl, I guess you can use its -p file test operator (perldoc.perl.org/functions/-X.html); something like perl -e 'exit(-p STDOUT ? 0 : 1);'
will tell you if stdout if a pipe or a fifo (without distinguishing between them).
Fwiw, calling that from the shell is exactly the reason why I used perl -e
:)