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.pl
1 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
    That's because when user enters his security number, he actually enters 12 or 13 characters - 11 for security number and one or two is the CRLF or just LF. You're reading only eleven, so CRLF is still in buffer when you trying to read EDP number, so readline getting end of security number line and returns it. Just use readline also for reading security number and it will remove CRLF from the buffer, you can check length and correctness of entered number later.
Re: Read & Readline Functions
by biohisham (Priest) on Oct 26, 2009 at 21:15 UTC
    The code you posted itself has got a lot of typos to be corrected, it is better you copy paste it than writing it a line at a time, at the regex line, you got {4), which one did you mean () or {}, then in the while loop where you checked the keys and values of the hash you apparently did not put another ) before {}

    There are many others, while I am not getting to answer your question, I am just showing you some things, and believe me, I can't stop myself from advising you to make a habit of typing:
    use strict; use warnings;
    at the top of your programs and getting to name your variables accordingly, the $name, is used only once to read from <STDIN>.. did this script work, did it give error messages?

    A visit to the documentation at readline and read and filehandles would help clarify some mysteries


    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.