Warning: As a general rule of thumb when learning programming, it's best that you don't copy/paste the code directly. You will learn much better if you type everything out multiple times. Trust me, it makes a night and day difference in how fast you will grasp the material.
If then statements in Perl are very similar to Java, Php, C, and various other If then statements. Start off with "if" and in parentheses state what the event will be. (In this tutorial we're going to check if a user's input matches a word. We will then print out a response that is relevant to the user's input) followed by a curly brace, what's going to happen on the event, and another curly brace.
To start off declare a variable and have it equal a standard input. Standard input will allow the user to type a response and will be written like this (<STDIN>).
PHP Code:
#!/usr/bin/perl
print "How was your day?:";
$response = <STDIN>;
chomp $do;
Make sure that you don't add a line break (\n) at the end if you want the standard input to appear right beside the printed text. Below you can find pictures of what it will look like with and without the line break. You can add a line break if it fits your needs. But for most uses it will look better beside the printed text.
You may have noticed that I added "chomp $do;" at the end. This will take what the user has typed in and remove any spaces at the beginning or end. This is not necessary, but could save you a few head aches.
Now that that's out of the way, it's time to get to the core of the lesson; if then statements. This is what our If then statement will look like. You can play around with the formatting if you wish to make your code a bit more compact. Like I said earlier. With perl there are many ways to do one thing.
Notice how we're using $response. That's called a variable, we'll be covering variables in Lesson #4. Also notice ho we're using "eq" to make sure that $response and "good" are equal. As you may have guessed "eq" stands for equals. If you're used to most other programming languages you may find this a bit strange, as you would normally use "==". That's because Perl was designed to be more like a human language, rather than a computer language. This may seem strange at first for c, java, php, etc programmers, but it will eventually start to make much more sense after a while. Also notice that the code is in parenthesis. Anything in the parenthesis will be your event. When the code in the parenthesis equals true, it will execute the code in the curly braces. make sure not to add a semicolon at the end of the event in parenthesis. If you add a semicolon Perl will think that the previous line of code is un-related to the next line of code and will produce an error. For if Then and Else statements only put the semi colon in the curly braces. As you may recall from the previous tutorial, you can also use single quotation. You'll only need to do this if the user will be typing in something that could be mistaken for Perl code.PHP Code:
if ($response eq "good")
{
print "\nGood to hear!\n";
}
Now, for what we want to happen when $response equals "good". Start off by making two curl braces. Any code you type in the curly braces will be executed when the event that was stated previously stated equals true.
If you wish to add another if statement to check if the variable equals something else specific it can be done with the following code.
PHP Code:
elsif ($response eq "bad")
{
print "\nLamesauce!\n";
}
And now for else statements. Else statements are very similar to if statements. Type "else" followed by two curly braces that contain code you wish to be executed on any event that makes the previous if statements false.
And now we have this as our completed code.
PHP Code:
#!/usr/bin/perl
print "How was your day?:";
$response = <STDIN>;
chomp $response;
if ($response eq "good")
{
print "\nGood to hear\n";
}
elsif ($response eq "bad")
{
print "\nLamesauce!\n";
}
else
{
print "\nInteresting\n";
}
If you have any questions or suggestions for future tutorials feel free to post below.
Results 1 to 1 of 1
- 20 Jan. 2011 11:22am #1
Perl lesson #3 (If, Then, and Else)
// Signature