Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

when

Perl 5 version 14.2 documentation
Recently read

when

  • when EXPR BLOCK

  • when BLOCK

    when is analogous to the case keyword in other languages. Used with a foreach loop or the experimental given block, when can be used in Perl to implement switch /case like statements. Available as a statement after Perl 5.10 and as a statement modifier after 5.14. Here are three examples:

    1. use v5.10;
    2. foreach (@fruits) {
    3. when (/apples?/) {
    4. say "I like apples."
    5. }
    6. when (/oranges?/) {
    7. say "I don't like oranges."
    8. }
    9. default {
    10. say "I don't like anything"
    11. }
    12. }
    13. # require 5.14 for when as statement modifier
    14. use v5.14;
    15. foreach (@fruits) {
    16. say "I like apples." when /apples?/;
    17. say "I don't like oranges." when /oranges?;
    18. default { say "I don't like anything" }
    19. }
    20. use v5.10;
    21. given ($fruit) {
    22. when (/apples?/) {
    23. say "I like apples."
    24. }
    25. when (/oranges?/) {
    26. say "I don't like oranges."
    27. }
    28. default {
    29. say "I don't like anything"
    30. }
    31. }

    See Switch statements in perlsyn for detailed information.