Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Test if STDOUT is attached to console or shell redirected filehandle

by liverpole (Monsignor)
on Sep 07, 2009 at 17:36 UTC ( [id://793995]=note: print w/replies, xml ) Need Help??


in reply to Re: Test if STDOUT is attached to console or shell redirected filehandle
in thread Test if STDOUT is attached to console or shell redirected filehandle

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$..$/

Replies are listed 'Best First'.
Re^3: Test if STDOUT is attached to console or shell redirected filehandle
by desemondo (Hermit) on Sep 09, 2009 at 00:49 UTC
    I think thats probably a Windows thing; when I call perl from the pipe first, your code works perfectly.
    fyi, I'm using ActiveState v5.8.8.822
    type highlight.pl | perl highlight.pl
      Interesting!

      In Windows (ActiveState v5.10.0) doing it this way "works":

      type highlight.pl | perl highlight.pl

      Doing it this way does not:

      type highlight.pl | highlight.pl

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://793995]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found