Perl Programming - Understanding STDIN in scaler context
Get the entire Perl Programming course for 20% off: http://stoneriverelearning.com/courses/perl-programming-for-beginners-online-course?product_id=38639&coupon_code=YOUTUBE20
Welcome back, guys! In this lecture, we’re going to be understanding standard input from our keyboard in scalar context. Again, this is section 4, lecture 9. Let’s jump right into it.
Understanding STDIN in scalar context. What is STDIN? Well, STDIN is also known as the line operator. The line operator or STDIN receives input or information typed in from our keyboard or the user’s keyboard. It returns the line of text from the user keyboard into our Perl program followed by a new line character at the end when the Enter key is pressed.
An example is just to declare our scalar variable and assign it our STDIN or our line operator. Always remember it’s in all capital letters STDIN inside our less than sign or our greater than sign or our diamond operators. After that, we want to remove the new line character. Of course, you add a left parenthesis followed by the variable name and we go down to print and we can actually print out the value inside our scalar variable that was typed in from the user keyboard. Let’s do some examples now.
First thing first, let’s start with a while loop. I’ll show you guys a really simple and quick way to set this up. We start off with our while loop followed by parentheses, skip a line. Let’s put our curly braces for our block of code. Inside the parentheses, let’s type a scalar variable. I’ll just name it line = STDIN. Once we do that, let’s use our favorite chomp operator. Again, what the chomp operator does is it just removes the new line character. I’ll do that. I’ll paste that in. Let’s actually print what’s inside our line variable or what we’ve previously typed from our keyboard. What this is going to do is it’s going to take our STDIN or our information from our keyboard, store it inside of our variable called line. It’s going to remove the new line character from our value or whatever we typed into our keyboard at the end of the line and we just want to reprint it. If we Run, click Run Script, this is how we know that it’s waiting for our standard input. You’ll see a blinking cursor in your shell terminal. I’ll type my name Victor Davis. If I press the Return or I press the Return button on my keyboard, what happens? It reprinted the line of text from our keyboard, Victor Davis. I’ll do it again Jaddian Forte and it does it again. Pretty much what it’s doing again it’s recording a line of text from your keyboard and just jumping down to our block of code because it’s true and it’s reprinting what we wrote. Again, I’ll type in Bobby Harris and it re-enters it again.
Pretty much that’s it for our STDIN. All it does is that it waits for information from the keyboard and once you press the Return key, it collects that information and we can save it into a variable which we can use that value for later. If you guys have any questions, please feel free to let me know. I’ll see you guys in our next lecture.