Perl: getting input from the user with STDIN
Tutoring, you often straddle a couple of generations. The tutor reaches back to an old programming tradition….
When I was a kid in the early 80s, computers were very new to have around. To make them more appealing to the home market – where they were suddenly available and even affordable – computers were given programs that allowed them to interact with the user. You’d see such situations as this:
Computer screen: Hello! What’s your name?
The user would type in: Gerald
Now the computer would reply:
Hello, Gerald! I’m so glad to meet you!
How did our predecessors manage that feat – and how can we do the same?
In Perl, you can do the following:
#!/usr/bin/perl
print “Hello. What is your name?\n\n”;
$usrname=<STDIN>;
chop $usrname;
print “\nHello, $usrname! I’m so glad to meet you!”;
<STDIN> means for the computer to gather input from the standard source; ie, the console.
Chop removes the newline (the ENTER stroke) Gerald pressed to “enter” his name.
Part of tutoring is continuing traditions:)
Source: Robert’s Perl Tutorial
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.