when is analogous to thecase keyword in other languages. Used with aforeach loop or the experimentalgiven block,when can be used in Perl to implementswitch/case like statements. Available as a statement after Perl 5.10 and as a statement modifier after 5.14. Here are three examples:
use v5.10; foreach (@fruits) { when (/apples?/) { say "I like apples." } when (/oranges?/) { say "I don't like oranges." } default { say "I don't like anything" } } # require 5.14 for when as statement modifier use v5.14; foreach (@fruits) {say "I like apples." when /apples?/; say "I don't like oranges." when /oranges?; default { say "I don't like anything" } } use v5.10; given ($fruit) { when (/apples?/) { say "I like apples." } when (/oranges?/) { say "I don't like oranges." } default { say "I don't like anything" } }See"Switch statements" in perlsyn for detailed information.
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.