Thank you ++graff for enlightening me to the usage of -t.

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

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.