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
In Perl, the given
-when
construct provides a way to perform switch-like conditional tests. It's somewhat similar to the switch
-case
constructs in other languages. To use given
-when
, you'll need Perl version 5.10 or newer, and you must use feature 'switch';
.
Here's a tutorial on how to use the given
-when
statement in Perl:
The basic syntax is as follows:
use feature 'switch'; given ($variable) { when (value1) { # Code for value1 } when (value2) { # Code for value2 } default { # Code if no values match } }
Let's consider an example where we want to determine the type of a variable:
use feature 'switch'; my $input = 3; given ($input) { when ("Hello") { print "It's a greeting!\n"; } when ([1,2,3]) { print "It's a number from 1 to 3!\n"; } default { print "I don't know what it is.\n"; } }
In this example, the script will print It's a number from 1 to 3!
since $input
is 3
.
You can also check for multiple conditions in a single when
:
given ($input) { when ([1,2,3,4,5]) { print "It's a small number!\n"; } ... }
break
By default, after a when
block is executed, Perl exits the given
construct. If you want to check subsequent conditions after a match is found, use continue
:
given ($input) { when (1) { print "It's one!\n"; continue } when ([1,2,3]) { print "It's a small number!\n"; } ... }
If $input
is 1
, this will print both "It's one!" and "It's a small number!".
The given
-when
construct utilizes Perl's smart match operator (~~
). The conditions inside when
are smart matched against the given
value. Smart match can compare scalars, arrays, and hashes in contextually appropriate ways.
It's also possible to use regex patterns:
given ($string) { when (/^Hello/) { print "The string starts with 'Hello'.\n"; } ... }
As of Perl 5.18, given
-when
is marked as experimental. While you can still use it, you might get warnings. It's always a good idea to check the current state of this feature in the Perl version you're using.
For a more traditional and less experimental approach, consider using cascading if
-elsif
-else
statements or the CPAN module Switch
.
The given
-when
construct in Perl provides a clean and readable way to test a variable against multiple conditions. It uses the smart match operator, allowing for a wide range of matching capabilities. However, be cautious about its experimental status in newer Perl versions.
Using switch-case in Perl with given-when:
given-when
construct in Perl allows you to create a switch-case-like structure for handling multiple conditions in a concise manner.my $value = 42; given ($value) { when (1) { print "One\n" } when (42) { print "The Answer to the Ultimate Question of Life, the Universe, and Everything\n" } when ([2, 4, 6]) { print "Even number\n" } default { print "Something else\n" } }
Perl smart matching with given-when:
~~
) is used in given-when
for flexible comparisons, allowing various types of matching.my $name = "Alice"; given ($name) { when (/^A/) { print "Name starts with A\n" } when ([qw(Alice Bob)]) { print "Name is Alice or Bob\n" } default { print "Other name\n" } }
Perl switch statement example:
given-when
construct in Perl serves as a switch statement, providing a concise way to handle multiple conditions.my $day = "Monday"; given ($day) { when ("Monday") { print "It's the start of the week\n" } when ("Friday") { print "Weekend is approaching\n" } default { print "Some other day\n" } }
Introduction to Perl given-when:
given-when
is a Perl construct that simplifies the syntax for creating switch-like structures for multiple condition handling.my $number = 7; given ($number) { when (1) { print "One\n" } when (7) { print "Lucky number\n" } default { print "Some other number\n" } }
Perl switch statement vs if-elsif-else:
given-when
provides a more concise syntax for handling multiple conditions compared to traditional if-elsif-else
structures.my $color = "green"; # Using if-elsif-else if ($color eq "red") { print "It's red\n"; } elsif ($color eq "blue") { print "It's blue\n"; } else { print "It's some other color\n"; } # Using given-when given ($color) { when ("red") { print "It's red\n" } when ("blue") { print "It's blue\n" } default { print "It's some other color\n" } }
Smart matching in Perl given-when:
~~
) in given-when
allows you to perform various types of matching, such as regex matching, array membership, etc.my $value = 42; given ($value) { when (/^\d+$/) { print "It's a positive integer\n" } when ([1, 3, 5, 7, 9]) { print "It's an odd number\n" } default { print "It's something else\n" } }
Perl switch case alternative:
given-when
construct serves as a switch-case alternative, providing a cleaner syntax for handling multiple conditions.my $fruit = "apple"; given ($fruit) { when ("apple") { print "It's an apple\n" } when ("banana") { print "It's a banana\n" } default { print "It's some other fruit\n" } }
Handling multiple conditions with given-when in Perl:
given-when
is effective for handling multiple conditions, especially when there are several distinct cases to consider.my $score = 85; given ($score) { when ($_ > 90) { print "Excellent\n" } when ($_ > 70) { print "Good\n" } default { print "Needs improvement\n" } }