Perl Lesson 2
That kids don't, no one here today. This is going to be Pearl lesson two of our Perl essence. So I'll just get started by working on a Perl script that I have already started. So it happens to be in my documents, so I called it "we", and this time I will be editing it with nano.
So anyway, here's my Perl script. Um, so I'll just, uh, I go into how to do if statements. So they've typed in their name in my little Perl script so far, which I called "the wii", and they've done all this crud. So now that I have their name and I've said hello to them, I can do an if statement.
Now, an if statement checks if something is true or not, so this could just be quite useful to you. So here are the three parts of an if statement: you type if
, space, open parentheses, and then you type, um, let's do $chisel
, which is the variable I assigned to their name, whatever they take into their name, space. Now I'm checking what it equals, so I'm going to type eq
, which stands for equals, space.
Well, let's check if their name is "Alex" close quote, close parentheses. So the thing is, the if statement is going to check for, and there's one more component of the statement: if
parentheses $chisel eq "Alex"
so that's if it equals lowercase "Alex" with no spaces after it, or anything like that, that's good.
So now after this, we want to do space, and then instead of a semicolon at the end of this line, we want to do an open curly brace. If you don't know what a curly brace is, you should watch my video on keys on your keyboard. Um, anyway, so we have that open curly brace.
So now go to the next line, and we can type on what will happen. So in this case, I'll print out something that says "Dude, you are the best." That's a shame. print "Dude, you are the best";
. So that's going to print out "Dude, you are the best" if they type their name as Alex. And I'll hit enter, and I'll do a close curly brace.
And now, say I want to check another thing. I do space, and then I do elsif
, except it's all one word and there's no "if" in "else". And then I do space, and then this is like the if. I just do open parenthesis $chisel eq "Gabe"
close parentheses. And then I do space, open curly brace.
Now I hit enter and I say "Hello, David;". And then quote semicolon. And now I hit enter and I can do a close curly brace. Um, I don't have to put in elsif
, but there has to be an open and close curly brace somewhere along the way.
So I have if $chisel eq "Alex"
and then I have this, and then I have a close curly brace. Then I just happened to throw in the elsif
after a close curly brace. You can either leave the if statement be or throw in an elsif
.
So now I'll throw in another elsif
right here, and I'll just zoom into this code so that you can see it better. And I'll also put all the code for this program I made in the description. So I did elsif $chisel eq "John"
and then one last thing they type in, if they type "John" will do another open curly brace.
Hit okay, I bet you love photo; now closed semicolon. Now I'll do a close curly brace there. Now say if they type anything else, so I want to do like an else and whatever. So I'll do space else
space open curly brace, and then I hit enter.
So after the close curly brace, I do space else
space open curly brace. I hit enter, now I'm just going to type print "I don't care about your name";. Now I'm going to do close curly brace.
So I have the standard if statement if (condition) {}
that I added in elsif
, I added another elsif
, and I added an else
. If statements can also just look like this: if ($chisel eq "Dave") {}
.
If statements can be as simple as this; that if statement is right here, pretty simple, unbelievably simple. I'll also put that simple if statement in the description. But you can also add as many elsif
as you want to, and of course, as many ifs. Or there's one if
, one else
, and you can add as many elsif
as you want.
Now, another thing I'll be teaching you in a short amount of time left is how to run a command in terminal from a Perl script. So let's say if it equals "Dave", just go to the next one, then I can type system "killall Terminal";
.
Gotta kill their terminal, and that's going to be really annoying. So now let's exit this as I would like to save, and I will just run "we." What is my name? First of all, I'll take "Dave."
I'll say "Hello, David," as I said it to act in the if statement. I've said to print out "Hello, David." So now remember the old trick you put backslash \n
at the end of every print if you wanted to go to the next line afterward.
So once again, you "we." Now I'm going to type random crud in here. It says "I don't care about your name." That's because I said in the else thing, if it doesn't equal any of those, to print out "I don't care about your name."
Right here it reminds me, I should add a backslash \n
right here. "Michael", ok? And then I can do "run we" again. Name in my program, and now I'm going to type "Alex." "Hello, Alex."
"Dude, you are the best!" John and I killed term. That is because I set it to kill terminal on the typed job. So John is really annoying me now because I quit the terminal.
So I did system "killall Terminal";
. And of course, I killed the terminal. I'll just do a better example of a system there. Let's think of one good command that would be sort of cool.
Let me go to my program, okay? So I'm going to edit my program again, and in the system here instead of "killall Terminal," I'm going to do something even more annoying. I'm going to type ls -lr /
; that’s going to display all the contents of the entire hard drive.
That's just a command in terminal you can try it out; I'll have it in this description. So now I can run "we" again, and it says "What is your name?" I'll just type "John."
"Oh no!" Get an ls -lr /
. So that's why it's so much fun to, um, do a Perl script. You can do a system, you can do if statements, so you can make basically a terminal emulator with the information I just gave you. So I thanks for watching, back as we rock, and goodbye.