Perl Programming – Getting User Input

Perl Programming – Getting User Input
Get the entire Perl Programming course for 20% off:
Welcome back, guys. In this lecture, we’re going to be looking at getting user input. Again, this is section 3, lecture 4.
How do we get the user input? Well, use the line input operator called stand STDIN to get input from the user. This is how we get a value from the keyboard into a Perl program. What’s really nice about this is that we can get this value from the user from their keyboard and we can save it into one of our containers or our variables which we looked at in our earlier lecture. Each time you use the standard input operator or line operator in a place where a scalar value is expected, Perl reads the next complete text line and uses that string as the value of standard input. So again, the standard input it waits until you hit the return key which gives us a new line character and it saves that complete line of text and we can save that into a variable again.
The string value of STDIN or from the line input operator typically has a new line character on the end of it where it ends. So let me show you some examples now. The first thing I like to do is start by declaring a variable because we want to save information that we can use later. I’m going to call this variable user input which makes sense because we want the user to enter information into our Perl program.
We’re going to use the line operator. So we put the less than sign and the greater than sign and inside that pair in all capital letters. We type STDIN. STDIN just stands for input from the keyboard. Now once we do that, we can go down to line 2. We can actually print what’s inside the user_input container or variable to see what the user has typed. So we use our handy-dandy print statement. What we can do is just copy our user_input variable. Paste it in to add a string to our output, followed by two new line characters. So

that will give us two new line characters. If we save our information, and we click Run, click Run Script, so what we see here that we know it works is that you’re going to see a blinking cursor waiting for your input. Perl is asking you to enter some information. It’s going to wait until you enter some information and then hit the return key or Enter key. So I’ll type my name, Victor Davis. Hit the Enter button. Look what we have. It reprinted my name.
Now, why did it do this? Of course, we have two new lines as well. So the first thing we look at is line 1. It waits for the user to start to enter some information from the keyboard. Once we hit the Enter key or the user hits the Enter key, it takes that line of characters or that string on that line and it stores that information inside our user_input. Of course we want to display what was typed by the user. So we take the variable that has the value and replaces our variable with its value. After the comma, we want to use two new line characters just to make it look a little bit nicer. That’s all it does. The standard input line operator it just gets information from the keyboard. It gets characters from the keyboard and we can save it and a variable for later use.”
Let’s do something a little bit more fancy. If we just erase this right now and I’ll put “Enter your name.” I’ll do that, so I’ll copy this line with text and I’ll put it on line 4. I want to read the person’s name. So I’ll do this and then I’ll add two more double quotes. I’ll put “Your name is ” and then after user_input, I’ll put more new line characters, so

. Save my work. I’ll click Run, Run Script to make it look a little bit nicer. I’ll put again Victor Davis. There we have it. If we look here, it says “Your name is Victor Davis” and it created two more new lines. That’s in a nutshell with standard input. We just get information from the keyboard into a variable and we can nicely print out some output. Again if you guys have any questions, I hope you guys learned something.


Mumtaz Shamsee says:

Your video got stuck

 Write a comment


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!