Perl 6 An Introduction to Perl 6 #27

Given / when ('switch' statements)

Any construct that sets the $_ topic (such as loops) can use 'when'.

# loop through @elems, execute first matching 'when' on each
for @elems {
    print "element $_ ";
    when 'spot'     { say 'is "spot"';        }
    when /spot/     { say 'contains "spot"';  }
    when Dog        { say 'has type Dog';     }
    when any(@cats) { say 'element of @cats'; }
    when none(@abc) { say 'not in @abc';      }
    default         { say 'unknown';          }
}
Copyright © 2008 Patrick Michaud