Hmm, this hangs when reading STDIN; got whatever
never gets printed.
#!/usr/bin/perl
use strict;
use warnings;
print STDOUT "stuff\n";
my $whatever = <STDIN>;
print "got whatever: $whatever";
exit;
I'm not too sure why it hangs: it seems to be that STDIN actually isn't tied to STDOUT, as you think it is. For example, we can prevent the above code from hanging forever by playing with alarm:
#!/usr/bin/perl
use strict;
use warnings;
$SIG{ALRM} = sub { die };
print STDOUT "whatever\n";
my $in;
eval {
alarm 1;
chomp ( $in = <STDIN> );
alarm 0;
};
print "got in: $in\n";
result:
whatever
Use of uninitialized value in concatenation (.) or string at foo.pl li
+ne 17.
got in:
- The first line, "whatever", is us printing to STDOUT.
- Next we get a warning about an uninitialized value - this is the warnings pragma in action, helpfully telling us that we're trying to print something that hasn't been set yet.
- Finally, there's a "got in: " line where we expect to see "got in: whatever".
So - what this means is that printing to STDOUT does not automagically make data available to STDIN. And that's good, too: imagine code that looks like this:
#!/usr/bin/perl
use strict;
use warnings;
print STDOUT "What is your name? : ";
chomp ( my $name = <STDIN> );
print STDOUT "Hello, $name!\n";
exit;
Now, without running the above code, tell me: what do you
expect the second print statement to print?
- Hello, What is your name? : blyman!
- Hello, blyman!
Yuck! Imagine if printing to STDOUT automagically made that data available to STDIN: you'd have to do some Olympian coding gymnastics to remove your STDOUT from your STDIN before you could find out what $name was entered as!
fwiw, I was confused about *why* STDOUT and STDIN aren't tied together - makes sense at first glance! - until I came up with the above code example. Hope this helps.
Update: realized greenFox was probably joking... but
by golly, I learned something.
blyman
setenv EXINIT 'set noai ts=2' |