I wrote some code where I thought I was testing for whether there was STDIN. But the code worked contrary to what I was expecting.
Here's the code I wrote, I've called it zit
#!/usr/bin/perl
use strict 'vars';
my @a = @ARGV ? @ARGV : "EMPTY";
printf " command line arguments: \"%s\" as expected\n", @a;
if ( -t STDIN )
{
print " ( -t STDIN ) returns TRUE\n";
}
else
{
print " ( -t STDIN ) returns FALSE\n";
print " But, I can iterate over <STDIN>! Huh?? Behold:\n";
}
Here's what I thought:
zit <(echo a;echo b)would result in( -t STDIN )beingFALSE.zit < <(echo a;echo b)would result in( -t STDIN )beingTRUE.
So, to try to figure out what might be going on, I modified the if-then code by adding a while loop (based upon my understanding, I put it where I thought it should create problems).
#!/usr/bin/perl
use strict 'vars';
my @a = @ARGV ? @ARGV : "EMPTY";
printf " command line arguments: \"%s\" as expected\n", @a;
if ( -t STDIN )
{
print " ( -t STDIN ) returns TRUE\n";
}
else
{
print " ( -t STDIN ) returns FALSE\n";
print " But, I can iterate over <STDIN>! Huh?? Behold:\n";
while ( <STDIN> )
{
print ">> $_";
}
}
Here's the output from this code
zit <(echo a;echo b) has the following output
command line arguments: "/dev/fd/63" as expected
( -t STDIN ) returns TRUE
zit < <(echo a;echo b) has the following output
command line arguments: "EMPTY" as expected
( -t STDIN ) returns FALSE
But, I can iterate over <STDIN>! Huh?? Behold:
>> a
>> b
I'm really confused by this. This is not behaving as I thought things should. If ( -t STDIN ) is false, why does the while loop work?
Could someone explain what's happening here?
I've re-edited this post from before to be a bit less confusing.