It is important to understand conditions and comparisons, which are a core concept of the Perl language.
Perl is a language developed by Larry Wall in 1987 when he was working for NASA as a systems administrator. He created the language with the sole purpose of making report processing easier. However, through the years, it has developed into something bigger, playing larger roles beyond its intended purpose.
Conditions and Comparisons
In order to learn the basic guide to files and strings, it is necessary to understand one more concept of the Perl Language.
If and Unless
All good programming languages allow you to be able to ask questions such as “Is this number greater than that number?” or “Are these two strings the same?” It will then act according to what the answer is.
Perl has four operators that it uses when dealing with numbers. These are <, >, == and !=. These symbols mean “less than,” “greater than,” “equal to” and “not equal to” respectively. The symbol <= can be used to denote “less than or equal to” and >= for “greater than or equal to”.
These operators can be used along with one of the conditional keywords in the Perl language, such as if and unless. These keywords will take a condition that Perl will test and Perl will run a block of code in curly brackets if the test works.
The conditional keywords if and unless works in the same way like their English equivalents. The if test will succeed if the condition is true and the unless test will succeed if the condition turns out to be false.
You can avoid a common bug by keeping in mind that there is a difference between = and ==. The former means “assignment” while the latter symbols mean “comparison for equality”. Be careful of coding these in so that you can save time for troubleshooting and looking for bugs.
Both the if and unless statements can be followed by an else statement should the test fail. It allows Perl to execute a code block whenever this happens. In order to chain together a bunch of if statements, you can use the keyword elseif.
While and Until
While and until are two slightly more complex keywords in the Perl language. Like the if and unless statements, they both take a condition and a block of code. The difference is that while and until act like loops that are similar to for. Perl will test the condition and run the block of code over and over again as long as the condition is true (when using the while loop) or until it becomes false (when using the until loop).
The most common string comparison which tests the string equality is eq. It tests if the two strings have the same value.
The keys == and eq may seem interchangeable but keep in mind that the former will tell Perl that you are dealing with numbers while the former will tell Perl that you are comparing strings.