Perl Lesson 3
Mids 101 here. Today, it's going to be another Perl lesson, so let's get started by typing in and editing a program I've already pre-made for you. Okay, so here it is, standard Perl stuff. As you can see right here, pretty simple code.
Okay, so now you can see a few things in my code. First of all, I did while true print "hi"; then a close curly brace. I'll just be demonstrating this to you by running this program and showing you what happens. So it keeps on printing out "hi." Now, why is this? Because I've done something called The Loop. A loop is something that keeps on, you know, doing one thing. For instance, if I kept on saying "hi" in a loop, I'd just be getting "hi hi hi hi hi hi hi." You probably know what a loop is already.
Um, so I'm going to be showing you how to do a loop in your Perl program. Let's get started by editing it now. It’ll see while (true) { print "hi"; } simple enough. You'd say while, and then you type a condition in here. I could do something other than true. While true just means do this while life is true is just a piece of crud.
Um, I could also, instead of true, do in here 3 == 3. That just means while 3 equals 3, do this. That means it'll do that again and again and again while 3 equals 3. Actually, when I do 3 equals 3, I have to do double equals, but whatever, and it'll keep on doing that.
So I can also put in here a condition. Watch this: print "Enter your name"; name = standard input; chom(name). Okay, and now I can say while $name eq "Alex." So it'll keep on printing out "hi" while my name is Alex. So I'll just type "Alex" is my name, and I'll keep on doing that. Press Ctrl+C to get out of there, by the way.
Now, to type my name, and I just typed some random crud, it hasn't done anything because it hasn't even entered that loop and begun to loop because my name was not Alex. So now let's try to do one more thing. Let's, um, just go down here.
Okay, so now we've done this. Okay, so what we want to do here now is we can do a few things. Um, we can just get rid of this name equals "Alex," or what we can say is, "while name not equal 'Dave.'" Let me just explain this. I've never said this before in an if statement or while loop or anything. If you have, you know, $name eq "something," you can also have "ne" instead of "eq," which stands for not equals instead of equals.
So I'll just say not equals "Alex." So now, all my name is not "Alex," it'll keep on doing that. Now, we can use this for more proper usages, like "Enter a command" and then $com = standard input; chom($com). Okay, and then let's do system($com). And then I'll say while $com doesn't equal "exit."
Okay, so now I can run this. Enter your name. I don't care what my name is. Enter a command. Now I can type "ls," and I'll do that. I can type "ls -lR /," and it'll do that. I can enter a command; it'll do that. But when I type "exit," it'll get out of the loop. You know why? Because the command that I'm processing is $com, and I said while $com doesn’t equal "exit."
So while $com doesn't equal "exit," if $com does equal "exit," it'll just get out of this loop. At the end of this loop, I'll just have it print "Exiting... Goodbye!" Okay, I'll put a few stars there just to make it official.
Okay, so, um now I'll just do $name. So now, I'll run "hi hi." Enter your name "Alex," and now it says "Enter command Alex." I can do "ls." I can do "ls /users/Alex/Desktop," and it'll show all the documents on my desktop, as accurately, you can see here.
Okay, um, so also you can, um, I'll just type "exit." You can add a condition here, so I'll get into this in my next tutorial, but just in case you care at all, after you do like $com not equal "exit," after that, you can do a space, insert, and then Ampersand (that is shift + 7), space, and then another condition.
I'll do $com not equal "quit." So it'll do this while $com doesn't equal "quit," and $com doesn't equal "exit." Now, $com can not equal "exit" but equal "quit." And actually, I need two shift sevens in there, two Ampersands. Anyway, so $com right here needs to either be "exit" or "quit."
If I've typed "exit" or "quit," this will do it while it doesn't equal it, and it doesn't equal "quit." So if it equals one of those, it'll just go out of the loop. To demonstrate this, of course, I will run it. Enter your name "Alex." I can type "ls." Now I can type "quit," and it's quit. I can also type "exit," and it's quit.
So that's just how to conveniently do a while loop. All the code for this description I believe will be in the description. Um, um, so thank you for watching, mates. Um, subscribe, and um, goodbye.