Cmdr Colstel has asked for the wisdom of the Perl Monks concerning the following question:
Thank you Perl brothers. I have a question regarding the use of the Readline function after previously using the Read function. The following script allows the user to enter a name using <STDIN> (line 10) and then allows entry of the user's social security number using the Read function. However, as you go to line 36 where the user is prompted for the class elective number the program does not allow for user entry. It skips right onto the next line not pausing for user entry. Can Read and <STDIN> be used in the same program or does something extra have to be done in order for one not to interfere with the other. It looks like the <STDIN> on line 37 has something already so that's why I'm not prompted for user input. I'm using Perl 5.10 on Win32 platform and running my script through Eclipse. Thank you.
elective 2.pl1 use PadWalker qw(peek_my peek our peek sub closed over); 2 3 print <<EOF; 4 REGISTRATION INFORMATION FOR SPRING QUARTER 5 Today's date is Wed Apr 19 17:40:19 PDT 2007 6 Please enter the following information: 7 Your full name: 8 EOF 9 10 $name = <STDIN>; 11 print "What is your Social Security Number (xxx-xx-xxxx): "; 12 read(STDIN, $social number, 11); 13 print "$social_number\n"; 14 if ($social number !~ m/"[\d] {3}-[\d] {2}-[\d] {4)$/) { 15 print "Not a valid social."; 16 exit; 17 } 18 else { 19 20 print <<INFO; 21 Your address: 22 Street: 1424 Hobart st. 23 City, State, Zip: Chico, CA 95926 24 25 "EDP" NUMBERS AND ELECTIVES: 26 INFO 27 28 %elective = ( "2CPR2B" => "c Language", 29 "lUNX1B" => "Intro to UNIX", "3SH414" => "Shell Programming", "4PL400" => "Perl Programming" ); 30 31 while( ($key,$value) = each(%elective) { 32 print "$key I $value\n"; 33 print x 30, "\n"; 34 } 35 } 36 print "\nWhat is the EDP number of the course you wish to take ? +:"; 37 chomp ($class_id = <STDIN>; 38 print "$class_id\n"; 39 print "You will be taking ",$elective{$class id}," this semester +.\n" 40 if exists $elective{$class id}; 41 print "This course number does not exist.\n" 42 if not exists $elective {$class id}; OUTPUT: REGISTRATION INFORMATION FOR SPRING QUARTER Today's date is Wed Apr 19 17:40:19 PDT 2007 Please enter the following information: Your full name: John Smith What is your Social Security Number (xxx–xx–xxxx): 101-42-4135 101-42-4135 Your address: Street: 1424 Hobart St. City, State, Zip: Chico, CA 95926 "EDP" NUMBERS AND ELECTIVES: 1UNX1B | Intro to UNIX ______________________________ 4PL400 | Perl Programming ______________________________ 2CPR2B | C Language ______________________________ 3SH414 | Shell Programming ______________________________ What is the EDP number of the course you wish to take ?: This course number does not exist.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Read & Readline Functions
by zwon (Abbot) on Oct 26, 2009 at 20:42 UTC | |
|
Re: Read & Readline Functions
by biohisham (Priest) on Oct 26, 2009 at 21:15 UTC |