Perl Lesson 1 (Summer Programing Course)
Hey guys, this is Mackins and one with a Perl lesson. Now I know I've had Perl lessons before, um, but they weren't that good. And so this is part of the summer agenda programming course. I'm going to be doing Perl. So Perl is our first interpreted programming language, and interpreted means that it doesn't compile into computer code that the computer itself knows how to handle.
Another program on your computer called Perl actually runs this Perl script that you write. So we're going to be using terminal to write our own little Perl programs that will, um, in this lesson, have you enter a number and, um, or how you enter something, you'll say that thing back and something else along with it. So you should watch other terminal lessons before, um, watching these Perl lessons. I think the first maybe five or six terminal lessons would do, but you should really know how to use terminal efficiently before you do these Perl lessons.
So I'm going to see to my desktop and I'm going to VI a program called hello world. And that is the file extension. You can also use Nano to edit a file, but I'm going to use VI, so I'll just hit enter and start editing my Perl program.
So the first line of every Perl program on Unix should be #!/usr/bin/perl. Now I'll hit enter, and now you want to type here. Here's how you, um, print something: print "Hello, what's your name? \n";
And then they'll type their name right there, and you need to end the line with the semicolon.
So now how do we declare a variable that's a string and get text into that string? That's easy, and in fact, declaring a variable, you don't have to do my $variable_name;
or my @array_name;
or whatever. You can just declare it. So you do $name = <STDIN>;
and then a value. So in this case, we're going to do $name = <STDIN>;
. And STDIN
is all caps.
So this is not only declaring a variable called name; it's assigning it to be <STDIN>
, which will read a line from a file. The file STDIN
is, um, or STDIN
in all caps, is the terminal. So I'm reading a line from the terminal. So whatever they type in, there's no limit to the amount of characters that can be put in this variable, and this variable is not at all a character array or a string or anything. It's just something stored in memory that you can treat as a number, a letter, or anything.
Then, just like when we did that thing with C, it is going to end with a new line. Now there's an easy way with Perl to get rid of that. You just type chomp($name);
, and then the variable name. The variable name always starts with either $
or @
. The @
sign means it's an array of variables, and $
means it's one variable. We're going to be using $
in all these Perl lessons.
So now I'll go to the next line and I'll say print "Hello, $name.\n";
So in the middle of a piece of text, you can put $name
, or the dollar sign and then the variable name, and instead of printing $name
, it'll print the contents of the variable.
So say we want to print something that has a dollar sign. So I'll say, "You're worth $10." Okay, so, um, say you want to make this dollar sign 10 a piece of text. Whenever there's a dollar sign, you just put a backlash before, and that makes it print out a dollar sign, not look for a variable name. So I'm putting out their name, and I'm saying they're worth $10.
So now I will run this. So the way to run any Perl program is you type perl
space and then the name of the program. And normally, that's a path to the program, so you can just type perl
space and then drag in the script.
And now it says, "What's my name?" I'll say, "Alex." "Hello Alex, you're worth $10." So that is how to write a hello world program. You can also rename it to something that doesn't have, um, what's it called, a .pl
extension, so it's just a text file. It'll open up with TextEdit then, and you'll be able to edit it more.
And if you want to make it executable, you can do chmod +x
space and then drag in the script, and then it'll automatically open the terminal. So if you've watched our other terminal lessons, it should be a snap for you. You should get it. You should be able to use Nano to edit the file, and then you should be able to type the code.
So this is how to use Perl to write any program. So, um, hopefully, you've understood this. If there's anything not clear, which I totally understand, please comment and or personal message me and ask me the question you have because I go very quickly in these lessons since YouTube has a time limit on their videos.
So thanks for watching and goodbye!