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'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.