Perl Programming – Creating a First Perl Program
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 again, guys. In this lecture, we’re going to be creating our first Perl program. How exciting! So let’s jump right into it.
You load up your Padre IDE or you double-click on that little pretty blue butterfly that says Padre, the Perl IDE, and you open that up. Of course, it will take us to our text editor by Perl. For our first line of code, we want to type the #!/usr/bin/perl. What this line of code does it just tells the compiler or our operating system where to open up our program or what program to use to run our program file. So it’s going to tell us “Hey, we’re going to open up our program and run it using our Perl program that we installed.” That’s all that this line of code does. This is usually for Unix-like environments or Linux systems and Mac OS X. This is where you’ll find your Perl program.
For our second line of code, let’s actually give it some space. We’re going to create our Hello, Perl. So type print(“Hello, Perl\n\n”); So just Ctrl + S that. Press Ctrl and S. Let’s just save it to our desktop for right now. So by default it’s called script.pl. Always make sure when you give a file name, when you’re creating a Perl program, it always uses the extension pl, so always end your file name with a .pl to let the system know that it’s a Perl program. Let’s type HelloPerl and save it. If we go up to Run which is located at the top of our IDE and we just click Run Script or a shortcut F5, let’s see what happens. Congratulations! Look what we have. Hello, Perl. Press any key to continue. And it closes out.
Let’s actually spice up this program a little bit because I don’t like the normal Hello, world and that’s it programs when you’re programming a new language. So let’s do a little bit more fancy thing. Let’s type another print statement. So type print(“what is your name: “); and followed by the next line. We type $name = STDIN; and let’s do another print command or a print instruction. Let’s type print(“Hello, “,$name, “\n\n”); and I’ll click Save. Let’s see what this does. If we click Run again, click Run Script, let’s see what happens. Hello, Perl. So now it’s going to ask us “what is your name” So you can type in your name. I’ll type in mine, Victor Davis. On our next line, it recognizes us. It says, “Hello, Victor Davis.” Wow, so the computer knows who we are now. Click Enter.
Just in a nutshell, and I’ll go over this in details in our further lectures, but a quick description of what the Print command does, it just prints and displays output to our monitor. For right now, that’s all we want it to do. We want to see something on our monitor of what the computer or the program is doing. In our next statement, it asked us again “what is your name” which we see on our computer monitor. For this, this $name is just a container, and with this STDIN followed by these brackets is going to ask us for input from our keyboard. Again, I’ll go over all this in detail in our further lectures. It’s going to say, “Hey, what is your name?” So I want you to type your name from the keyboard and we’re going to save that name into a container called Name. Again, what we did here was just use another print command saying, hey, I want to see some output saying Hello followed by whatever the user put for their name and with these \n\n, it just creates a new line. So it goes down to the next line inside of our output.
So again, we can run this again. Let’s press F5. So Hello, Perl. I’ll just type in Joe Blow. Again, it’s doing exactly what we instructed it to do. Hello, Joe Blow. That’s pretty much all of our code that we have in our Perl program. We are the best Perl programmers now in the world. So congratulations, guys, if that worked. Congrats! Of course again I’ll explain all of this in our further lectures and details. We’re going to be seeing a little bit more advanced examples. You can just follow along. You don’t have to type it out. I’ll just show you personal examples and more advanced Perl applications that I’ve built to kind of give you a heads-up of what we’ll be doing in this course.