yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Perl Lesson 2


5m read
·Nov 3, 2024

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.

More Articles

View All
Lisa Bodell: Why Organizations Resist Change | Big Think
What holds us back from better embracing change might not be what you think. And I think it’s two things. The first thing is mindset. And what we always find with leaders is we can’t tell them that they’re the ones that are holding us back from change; we…
Bullets HITTING Bullets in Slow Motion - THE IMPOSSIBLE SHOT - Smarter Every Day 287
Three, two, one. Fire (BANG!) Hey, it’s me, Destin. Welcome back to Smarter Every Day. I am at the American Museum of Natural History. It’s a Smithsonian Museum. And this is something that I saw ten years ago, and it changed the way I think about bullets …
Fishing Tips: How to Handline | Wicked Tuna: Outer Banks
[Applause] [Music] [Applause] Hand lining is what we used to do years ago, 30 years ago, before Tyler was born, before all these guys were even on a boat fishing. We used to do handlines; now they do rod and rails. They just crank. It’s very important wh…
Adding and subtracting fractions with negatives | 7th grade | Khan Academy
Let’s say we wanted to figure out what (3 \frac{7}{3}) minus (-\frac{7}{3}) minus (\frac{11}{3}) is. Pause this video and see if you can have a go at it before we do it together. All right, now let’s work on this together. You might be tempted to deal wi…
Get Ready for the Asteroid Gold Rush | Big Think
We had the idea that we could mine asteroids for over a hundred years. Late 1800s is when we first started thinking about this in the first place. And today we have, I think it’s three or four different asteroid mining companies. It’s no longer an idea. W…
Carl Sagan Changed My Life: Bill Nye on Chuck Berry, Kids' Love of Science, and Voyager Spacecraft
[Music] Hello! No matter the science guy. I am a guy. I’m a YouTuber named Wesley, and I wanted to ask you if, since Carl Sagan was your mentor, if I’m not mistaken, what is your fondest memory with your mentor, Carl Sagan? Take care and have a good day. …