Perl Programming/Keywords/given

Previous: getsockopt Keywords Next: glob

The given keyword

edit

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

edit
  EXPRESSION when EXPRESSION
  given EXPRESSION

Examples

edit
use v5.10.1;

given ($var) {
  when (/^abc/) { $abc = 1 }; continue;
  when (/^def/) { $def = 1 }
  when (/^xyz/) { $xyz = 1 }
  default       { $nothing = 1 }
}

See also

edit
Previous: getsockopt Keywords Next: glob