Perl - Asking Questions and Remembering the Result

Well, the homework assignment from the last lesson was to consider the programming elements that you would need to accomplish the task of having a Perl program call you by your own name.  The way I figured it, I needed

  1. a place to hold the name,
  2. a way to ask for the name, and
  3. a way to get a response.

One kind of place to hold values (like a name) is a scalar variable.  As we loop through the lessons of this workshop, we'll be revisiting concepts like "scalar variable" at least a couple of times.  So, we'll be going into more detail about what these variables can hold, and what you can do with them.  For now, assume that you can hold a single number (with, perhaps, as many as seventeen digits - it depends upon the O/S that the Perl interpreter is running on [try it]) or string (sequence of characters [zero {or null}, minimum, to 255, maximum]) in a scalar variable and that a scalar variable name always begins with a "$".  The "$" symbol works sematically like the English word "the" in that it indicates that a single value is expected.  (Note that the first mention of "scalar variable" above [and wherever mentioned in this tutorial] is a hyperlink which you can click on to get a full discussion of this topic.)  Hey, why don't we use a scalar variable name of $name to hold your name (or "the" name).

Well, the program also has to be able to ask for your name.  To do that, we need a way to prompt and a way to accept input.  Remember our "Hello World" program?  It actually showed us a way to prompt, didn't it:  use the print function.  And the way to get a response from the keyboard is with the <STDIN> (standard input) construct, which (as we will use it here) grabs a line of input from the keyboard.  We'll assign this input to the $name variable.  All of this rumination about programming elements results in the following:

print "What is your name? ";
$name = <STDIN>;

The value of $name at this point also includes a terminating newline character (the "record separator" for <STDIN> to "know" that it has one line of input).  To get rid of that newline character, we can use the chomp function, which takes a scalar variable as its sole argument and removes the trailing newline, if present, from the string value of the variable.

chomp ($name);

You gotta love "chomp"!

Now all we need to do is say Hello, followed by the value of the $name variable, which we can do by embedding the variable name inside the quoted string (along with the newline "character" [\n]):

print "Hello, $name!\n"

Note:  What if you wanted to print a real dollar sign rather than a scalar variable reference?  Then you would have to precede the dollar sign with a backslash (this is called "escaping" the character).  I believe we'll see one or more examples of this in lessons which follow in this workshop.

Putting it all together, we get:

#!/usr/bin/perl -w
print "What is your name? ";
$name = <STDIN>;
chomp ($name);
print "Hello, $name!\n";

In the next lesson we're going to add even a bit more sophistication to this program by having it give a special greeting to you, but just an ordinary greeting to anyone else!  And, your homework assignment, should you decide to accept it, is to, not only consider the programming elements that you would need to accomplish this task, but also to describe what crucial element is missing from the program above.  This tape will self-destruct in 30 seconds . . . .