Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Decision-making is an essential part of any programming language. In Perl, decision-making structures help determine the direction in which the program's flow should move. Let's dive into these structures:
if
StatementThis evaluates a condition. If the condition is true, the block of code inside the if
statement is executed.
my $number = 10; if ($number > 5) { print "$number is greater than 5\n"; }
if-else
StatementThis allows you to execute one block of code if the condition is true, and another block if the condition is false.
my $number = 3; if ($number > 5) { print "$number is greater than 5\n"; } else { print "$number is less than or equal to 5\n"; }
if-elsif-else
StatementFor situations with multiple conditions, elsif
can be used:
my $number = 5; if ($number > 10) { print "$number is greater than 10\n"; } elsif ($number < 5) { print "$number is less than 5\n"; } else { print "$number is between 5 and 10 (inclusive)\n"; }
unless
StatementThis is the opposite of the if
statement. The code inside the unless
block is executed if the condition is false.
my $number = 4; unless ($number > 5) { print "$number is not greater than 5\n"; }
You can also combine unless
with else
:
unless ($number > 5) { print "$number is not greater than 5\n"; } else { print "$number is greater than 5\n"; }
given-when
Structure (similar to switch-case
in other languages)To use this structure, you'll need to include the feature
pragma for versions of Perl 5.10 through 5.18. However, it's worth noting that starting from Perl 5.18, given-when
is considered experimental and might produce warnings. You might want to consider using the Switch
module or an if-elsif
chain for a more stable solution.
use feature 'switch'; my $value = 'B'; given ($value) { when ('A') { print "It's A\n"; } when ('B') { print "It's B\n"; } default { print "It's neither A nor B\n"; } }
? :
)This is a shorthand for if-else
. It's useful for short evaluations.
my $number = 7; my $result = ($number > 5) ? "$number is greater than 5" : "$number is less than or equal to 5"; print $result;
0
, '0'
, ''
(empty string), undef
, and ()
(empty list) evaluate to false
. Everything else is true
.if
and unless
statements are mandatory.given-when
is experimental in newer versions of Perl and might produce warnings.By understanding and applying these decision-making structures, you can create complex logic flows tailored to your Perl application's needs.
If statements in Perl:
my $value = 10; if ($value > 5) { print "Value is greater than 5\n"; }
Perl else if and else statements:
my $value = 10; if ($value > 15) { print "Value is greater than 15\n"; } elsif ($value > 5) { print "Value is greater than 5 but less than or equal to 15\n"; } else { print "Value is 5 or less\n"; }
Switch statements in Perl:
my $day = "Monday"; given ($day) { when ("Monday") { print "It's the start of the week\n"; } when ("Friday") { print "Weekend is near\n"; } default { print "Some other day\n"; } }
Comparisons and conditions in Perl:
<
, >
, <=
, >=
, ==
, !=
) for evaluating conditions.my $x = 5; my $y = 10; if ($x < $y) { print "$x is less than $y\n"; }
Ternary operator in Perl:
? :
) allows for concise conditional expressions.my $value = 10; my $result = ($value > 5) ? "Greater than 5" : "5 or less"; print "$result\n";
Logical operators in Perl decision-making:
&&
, ||
, !
) are used for combining and negating conditions.my $x = 5; my $y = 10; if ($x > 0 && $y > 0) { print "Both values are positive\n"; }
Nested if-else in Perl:
my $x = 5; my $y = 10; if ($x > 0) { if ($y > 0) { print "Both values are positive\n"; } else { print "\$y is not positive\n"; } } else { print "\$x is not positive\n"; }
Perl case-insensitive comparisons:
lc
or uc
functions to make comparisons case-insensitive.my $string1 = "Hello"; my $string2 = "hello"; if (lc($string1) eq lc($string2)) { print "Strings are equal (case-insensitive)\n"; }