I've always wondered if it were possible to detect whether a script were being run on the receiving end of a pipe (somehow I never came across -t). Using -t with <STDIN> is exactly how to do it!
Here's a test script highlight.pl which, in a Linux terminal window, demonstrates the difference between:
% ./highlight.pl
and:
% cat highlight.pl | ./highlight.pl
#!/usr/bin/perl -w use strict; use warnings; my $b_windows = ($^O =~ /win/i)? 1: 0; if ($b_windows) { require Win32::Console::ANSI; print "\e[H\e[J"; } my $color1 = $b_windows? "\e[1;42m": "\e[102m"; my $color2 = $b_windows? "\e[1;45m": "\e[105m"; my $a_lines = [ "# Stopping By Woods On A Snowy Evening -- Robert Frost", "# ", "# Whose woods these are I think I know.", "# His house is in the village though;", "# He will not see me stopping here", "# To watch his woods fill up with snow.", "# ", "# My little horse must think it queer1", "# To stop without a farmhouse near", "# Between the woods and frozen lake", "# The darkest evening of the year.", "# ", "# He gives his harness bells a shake", "# To ask if there is some mistake.", "# The only other sound's the sweep", "# Of easy wind and downy flake.", "# ", "# The woods are lovely, dark and deep.", "# But I have promises to keep,", "# And miles to go before I sleep,", "# And miles to go before I sleep.", ]; my $pattern = "wood"; if (-t STDIN) { # Program being run standalone: "<script>" foreach (@$a_lines) { s/($pattern)/$color1$1\e[m\e[K/gi; print " $_\n"; } } else { # On receiving end of another process: "cat <script> | <script>" while (<STDIN>) { if (/"# [a-zA-Z]/) { s/($pattern)/$color2$1\e[m\e[K/gi; print; } } }
Apparently the -t <filehandle> construct doesn't work on Windows (at least not my version (update: ActiveState v5.10.0), because in both cases -t STDIN evaluates to FALSE. That is:
C:\> highlight.pl
does the same thing as:
C:\> type highlight.pl | highlight.pl
In reply to Re^2: Test if STDOUT is attached to console or shell redirected filehandle
by liverpole
in thread Test if STDOUT is attached to console or shell redirected filehandle
by desemondo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |