in reply to Re: Testing Input and Output
in thread Testing Input and Output
#!/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:
result:#!/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";
whatever Use of uninitialized value in concatenation (.) or string at foo.pl li +ne 17. got in:
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:
Now, without running the above code, tell me: what do you expect the second print statement to print?#!/usr/bin/perl use strict; use warnings; print STDOUT "What is your name? : "; chomp ( my $name = <STDIN> ); print STDOUT "Hello, $name!\n"; exit;
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'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Testing Input and Output
by greenFox (Vicar) on Sep 04, 2002 at 01:16 UTC |