Perl/Basics/Boolean Logic
Contents
Operators
Mathematical
- =
The = operator assigns a value to a variable.
- ==
The == operator is used to test if a variable is equal to a value or another variable.
- !
The ! operator means "not". It applies to = and any variable. For example, "if (!$scalar)" is used to determine if the $scalar variable is null, zero, or undefined. When used before a = operator, this becomes "not equal to".
- eq
Sometimes because variables may not be a string or integer, the eq operator is used to determine if the two are equal.
The following operators are used for greater than, less than, and greater than or equal to, less than or equal to, etc; similar to other languages:
- >
- gt
- <
- lt
- >=
- gte
- <=
- lte
Regular Expressions
The ~ operator is used with regular expressions, which are covered later in this article. The ~ operator can be used in a variety of ways:
- =~
- !~
Regular expressions can also be very useful when using perl as a shell glue language. As an example:
"find | perl -nwl -e "m:zs: and print"
will pipe the output of the find command into perl, which will then apply the regular expression m:zs: which is an expression which only looks for those two characters in those orders. The use of "and" makes perl only apply the print operation to the line if the first match returns a "true." The use of -nwl means that newlines are stripped, warnings are enabled, and that the input which doesn't match isn't printed as well.
Statements
if
An if statement may have 3 types of clauses: if,elsif, and else. For the below example, assume that the $age scalar is passed as a command line argument:
if (int($age) == $age) { #Making sure it's an integer. if ($age < 18) { #If the age is less than 18: print "You must be at least 18 to view this.\n"; } elsif ($age < 21) { # If the age is more than 18, but less than 21: print "Because you are under 21, some features may be restricted.\n"; } else { # If none of the conditions have been met: display_content(); } } |
unless
An unless statement may only have the unless clause and an else clause.
unless ($age >= 21) { print "All content is restricted for users under the age of 21.\n"; } else { print "Welcome to our sample age gate!\n"; display_content(); } |
AND and OR
"And" and "or" are used to apply multiple conditions to a boolean statement.
- && is the way perl represents "and"
- || is the way perl represents "or"
Example:
if ($age < 21 && $age >= 18) { print "Some content will be restricted because you are not older than 21.\n"; } |
switch
To use perl's switch() routine you must have use Switch; before your switch() statement. A switch statement allows a programmer to avoid long chains of "elsif" statements. It condenses the amount of required lines of code. Perl's switch statement is very similar to the switch() statement in C and C++, though the syntax is a little different. A perl switch() statement may contain case and else clauses. Perl switch cases can also be used to determine if a value is in a list, an array element, hash key, or matches a regular expression or string. In this example, suppose $option was a numeric value for an integer based menu with 3 options.
use Switch; switch(int($option)) { case 1 { # Essentially the same as if ($option == 1) print "You picked option 1!\n"; } case 2 { # Essentially the same as elsif ($option == 2) print "You picked option 2!\n"; } case 3 { # Essentially the same as elsif ($option == 3) print "You picked option 3!\n"; } else { print "invalid menu option!\n"; } } |
For more information, see perldoc switch, or: here.
Golfing
The term golfing applies to condensing a boolean statement into one line. Golfing is typically used when you only need to execute one line of code for a boolean statement.
print "You are not 18 or older!\n" unless ($age >= 18); |
- Is essentially the same as:
print "You are not 18 or older!\n" if ($age < 18); |
- Is essentially the same as this un-golfed statement:
unless ($age >= 18) { print "You are not 18 or older!\n"; } |
Helper Natives
These helper natives are boolean statements that assist with the determination of the existence of or the defining of a variable.
exists
The exists native applies specifically to hashes and hash references.
print "This user has an age.\n" if exists $user->{'age'}; |
defined
The defined native determines if a scalar value is defined:
print "We received a response from the server.\n" if defined $response; |
undef
The undef native determines if a scalar value is un-defined:
print "We received a response from the server.\n" unless undef $response; |
Bitwise Manipulations
Perl's bitwise manipulations cover the syntax for performing bitwise math on variables.
AND
- & - The AND operator.
my $num = 10; $num = $num & 25; print $num . "\n"; |
NOT
- ~ - The NOT operator
my $num = 10; $num = ~$num; print $num . "\n"; |
OR
- | - The OR operator
my $num = 10; $num = $num | 25; print $num . "\n"; |
XOR
- ^ - The xor (exclusive or) operator
my $num = 10; $num = $num ^ 25; print $num . "\n"; |
Bit Shifting
- << - The shift left operator
- >> - The shift right operator
<syntaxhighlight lang="perl">my $num = 10; $num = $num << 2; #Shift left two bits $num = $num >> 2; #Shift right two bits print $num . "\n"; </syntaxhighlight> |
Bit Rotation
Perl bit rotation requires the Bit::ShiftReg package from CPAN. More information available there.