< Perl Programming < Keywords
The given keyword
given is a highly experimental flow-control keyword that is used in a similar manner like the switch keyword in C.
The given loop is supported from Perl 5.10.1 onwards. This can be used with the use feature "switch" or with the use 5.10.1 command. With the given command, the other experimental keywords break, continue, default, and when are also enabled. From Perl 5.16 onwards, it is also possible to use these keywords with the CORE:: prefix and without using the use statements.
In contrast to the switch statement in C with the case parts, the given switch leaves after each when statement without needing a break keyword. If this is not desirable, the continue has to be used.
Syntax
EXPRESSION when EXPRESSION
given EXPRESSION
Examples
use v5.10.1;
given ($var) {
when (/^abc/) { $abc = 1 }; continue;
when (/^def/) { $def = 1 }
when (/^xyz/) { $xyz = 1 }
default { $nothing = 1 }
}
See also
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.