Allasso has asked for the wisdom of the Perl Monks concerning the following question:
It receives user input the first time, but prints and exits the script without letting me receive user input the second time. I tried undefining $/ in a separate block, and just seeing if I could read single line input the second time, but it does the same thing - just does the first read, then exits:#!/usr/bin/perl use strict; my $input; local $/; $input = <STDIN>; print $input; $input = <STDIN>; print $input;
this doesn't work either:#!/usr/bin/perl use strict; my $input; { local $/; $input = <STDIN>; } print $input; $input = <STDIN>; print $input;
This works though:#!/usr/bin/perl use strict; my $holdRS = $/; local $/; my $input = <STDIN>; $/ = $holdRS; print $input; my $holdRS = $/; local $/; my $input2 = <STDIN>; $/ = $holdRS; print $input2;
What is it about undefining $/ that allows me to read user input only one time? (even if I set it back to default first)#!/usr/bin/perl use strict; my @userinput = <STDIN>; foreach (@userinput) { print; } my @userinput = <STDIN>; foreach (@userinput) { print; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to read STDIN interactively
by ikegami (Patriarch) on Feb 24, 2011 at 15:54 UTC | |
by Allasso (Monk) on Feb 24, 2011 at 21:02 UTC | |
by ikegami (Patriarch) on Feb 24, 2011 at 21:39 UTC | |
by Allasso (Monk) on Feb 25, 2011 at 01:14 UTC | |
by ikegami (Patriarch) on Feb 25, 2011 at 01:47 UTC | |
| |
by DrHyde (Prior) on Feb 25, 2011 at 10:48 UTC | |
by Allasso (Monk) on Feb 24, 2011 at 21:03 UTC | |
|
Re: how to read STDIN interactively
by Corion (Patriarch) on Feb 24, 2011 at 15:55 UTC | |
by ikegami (Patriarch) on Feb 24, 2011 at 16:11 UTC | |
|
Re: how to read STDIN interactively
by DrHyde (Prior) on Feb 25, 2011 at 10:29 UTC |