Up to this point, with only one exception, you have conducted **instantiative** programming. That is to say, your script starts at the top, goes one line at a time until it returns a `true` or `false` statement saying it was successfully completed. But sometimes you need to control which parts of your script are actually run, when they are run, and even how they are run. Code blocks start that process. A code block is a small section of code that will only run in given circumstances. In Perl, code blocks are indicated with the curly brackets `{ }`. You've already used one when making your hash from an [[Retrieving User Input from HTML POST requests|HTML form]]. In it you conducted the following; ``` perl for $pair (@pairs) { chomp($pair); ($key, $value) = split('=', $pair); $FORM{$key} = $value; } ``` The code block was all the code inside the `{ }` brackets. That code block was run for every entry in `@pairs`, and we called each one of those entries `$pair`. That is an example of a loop code block. # Conditions A condition is a code block that occurs under specific circumstances. In it's most basic form it requires a **boolean** (a true or false value, usually represented in Perl with a `1` for true and `0` for false) to determine whether or not it should run the code block, or skip it. ``` perl if (1) { # your script instructions } if ($myBoolean) { # your script instructions } if ($number1 == $number2) { # your script instructions } ``` In the example above, the first `if` statement always returns true, so it will always run its code block. The second `if` statement will only run its code block if `$myBoolean` has a true value. The third `if` statement will only run its code block if the two numbers returned by `$number1` and `$number2` are identical. `if` also has an opposite; `unless`. In the case of `unless`, if the *boolean* returns true, it will skip the code block instead. ``` perl unless ($myName eq 'John Jacob Jingleheimer Schmidt') { print "Who are you?\n"; } ``` In that example, the script will only ask who you are if `$myName` is not exactly equal to the string 'John Jacob Jingleheimer Schmidt'. `if` also has optional conditions that `unless` does not have. They are `if` ... `elsif` ... `else`. Each one has a code block that runs only if its conditions have been met after every previous condition hasn't been met. You can have as many `elsif` optional conditions chained as you like. ``` perl $favColor = 'turquoise'; if ($favColor eq 'black') { print "Paint it Black"; } elsif ($favColor eq 'blue') { print "I'm Blue da ba dee da ba die"; } elsif ($favColor eq 'red') { print "Red Red Wine"; } else { print "You're weird"; # this is what gets printed this time. } ``` In the case above, the script traverses all the conditions in the chain until it finds one that works. In this case, it didn't, so the `else` condition is what kicked in. # Comparison Operators Comparison operators are those that compare the value on the left to the value on the right, such as `$string1 eq $string 2` where `eq` is the comparison operator. Comparison operators will return true if the comparison matches, and false if not. There are two classes of Comparison Operators; one that works on strings, and one that works on everything else. ## String Comparisons The two main string comparisons are `eq` and `ne` representing "equal to" and "not equal to". They are byte-for-byte comparisons, so if one value is only different by a space, or period or even an invisible `\n`, then they won't be equal. ``` perl $string1 = "Hello"; $string2 = "Hello"; $string3 = "hello"; $string4 = "Hello\n"; $string1 eq $string2; # returns true $string1 eq $string3; # returns false $string1 eq $string4; # returns false $string2 ne $string1; # returns false $string2 ne $string4; # returns true ``` ## Equality Operators Equality Operators compare numerical values. As booleans are even represented as `1` or `0`, Perl will interpret them as numerical values. The following equality operators are very common; - `==` is exactly equal to - `!=` is not equal to - `>` is greater than - `<` is lesser than - `>=` is greater than OR equal to - `<=` is lesser than OR equal to ### The special case of equals (`=`) A single equals sign is not a comparison operator. It is an assignment operator. It's job is to put a value inside something else. If it successfully assigns a value, it will always return success, which is always the true boolean `1`. So ... ``` perl $myNumber = 3; # returns 1 because successfully assigned the number $yourNumber == 2; # returns 0, because $yourNumber has never been created $theirNumber = 1; # returns 1 because successfully assigned the number $myNumber != $theirNumber; # returns 1 because they are not equal ``` In fact, depending on the Perl installation you have, the first time a statement returns a 0 the script might simply fail. **Rule: Do not use a single `=` when trying to compare.** # And, Or, Not Sometimes one boolean statement isn't enough. Sometimes you need to test 2 or even more different booleans. This is where `&&`, `||`, `AND`, `OR` and `!` come into play. - `&&` means the boolean on the left AND the boolean on the right must return true. - `||` means the boolean on the left OR the boolean on the right must return true. - `AND` is similar to `&&` but in the order of operations, it is the last AND considered. - `OR` is similar to `||` but in the order of operations, it is the last OR considered. - `!` is placed in front of the boolean to negate it, or to say NOT. - `!(1)` will return `0` - `!(0)` will return `1` ``` perl if ($color1 eq 'red' && $color2 eq 'blue') { print "purple"; } elsif ($color1 eq 'red' AND $color2 eq 'green') { print "yellow"; } elsif ($color1 eq 'red' || $color2 eq 'red') { print "red"; } elsif ($color1 eq 'red' OR !($color2 eq 'red')) { print "green"; } elsif (!($color1 eq 'red')) { print "blue"; } else { print "black"; } ``` # Assignment - Supervillain Certification Exam The premise: The International Association of Supervillains has hired you to develop its new computerized certification system. Every aspiring supervillain must complete an examination. Your program will ask questions about their villainous qualifications, evaluate their answers, and determine their official supervillain classification. Unfortunately, the Association's standards are somewhat questionable. ## Part 1 — Collect the applicant's information Your program must ask four questions: |Question|Expected input| |---|---| |How impressive is your evil laugh?|0–10| |How many minions do you command?|Any nonnegative integer| |Do you own a secret lair?|1 = Yes, 0 = No| |Do you own a dramatic cape?|1 = Yes, 0 = No| You can begin with a simple Perl program: ``` perl #!/usr/bin/perl print "SUPERVILLAIN CERTIFICATION EXAM\n"; print "================================\n\n"; print "Evil laugh rating (0-10): "; $laugh = <STDIN>; chomp($laugh); print "Number of minions: "; $minions = <STDIN>; chomp($minions); print "Do you own a secret lair? (1/0): "; $lair = <STDIN>; chomp($lair); print "Do you own a cape? (1/0): "; $cape = <STDIN>; chomp($cape); print "\nEvaluating your evil potential...\n\n"; # Your certification logic goes here! ``` ## Part 2 — Make the decisions The Association has established the following certification rules. Students must translate them into an `if ... elsif ... else` structure with the following possible results; - *Secretly a Hero* - If the applicant's evil laugh is exactly 0 AND they have exactly 0 minions, they are clearly not a villain. - *Supreme Overlord* - If the applicant's evil laugh is at least 8 AND they either own a secret lair OR command at least 5 minions, they qualify for the highest certification. - *Licensed Menace* - If their evil laugh is at least 5 AND their cape ownership is NOT 0, they receive an intermediate certification. - *Evil Intern* - Everybody else is assigned an unpaid internship in the Department of Mild Inconveniences. ## Part 3 — Test your program Use the following as a way of testing your logic. | Laugh | Minions | Lair | Cape | Expected result | | ----- | ------- | ---- | ---- | ---------------- | | 0 | 0 | 1 | 1 | Secretly a Hero | | 9 | 6 | 0 | 0 | Supreme Overlord | | 10 | 0 | 1 | 0 | Supreme Overlord | | 7 | 2 | 0 | 1 | Licensed Menace | | 3 | 4 | 0 | 0 | Evil Intern | | 9 | 0 | 0 | 1 | Licensed Menace | That last example is particularly important: a fantastic evil laugh and an excellent cape aren't enough to become Supreme Overlord. ## What the terminal experience could similar to ``` bash Terminal — perl villain.pl SUPERVILLAIN CERTIFICATION EXAM ================================ Evil laugh rating (0-10): 9 Number of minions: 12 Do you own a secret lair? (1/0): 0 Do you own a cape? (1/0): 1 Evaluating your evil potential... CERTIFICATION: SUPREME OVERLORD Congratulations! Your villainous credentials are impeccable. Please remember to submit your quarterly evil reports by Friday. ```