in reply to Re^6: Understanding the benefit of Given/When ...
in thread Understanding the benefit of Given/When ...

You can put whatever code you want between various when blocks. You can also have more than one default - a default is just a when with a condition that's always true:
my @test = qw [abc def ghi xyz jkl mno]; for (@test) { say "Testing '$_'"; when (/abc/) {say " Got 'abc'"; continue} say " After when /abc/"; when (/def/) {say " Got 'def'"; next;} say " After when /def/"; when (/ghi/) {say " Got 'ghi'";} say " After when /ghi/"; when (/jkl/) {say " Got 'jkl'"; last;} say " After when /jkl/"; default {say " Default 1"; continue} say " After first default"; default {say " Default 2";} say " After second default" } continue { say " Continue for '$_'"; } __END__ Testing 'abc' Got 'abc' After when /abc/ After when /def/ After when /ghi/ After when /jkl/ Default 1 After first default Default 2 Continue for 'abc' Testing 'def' After when /abc/ Got 'def' Continue for 'def' Testing 'ghi' After when /abc/ After when /def/ Got 'ghi' Continue for 'ghi' Testing 'xyz' After when /abc/ After when /def/ After when /ghi/ After when /jkl/ Default 1 After first default Default 2 Continue for 'xyz' Testing 'jkl' After when /abc/ After when /def/ After when /ghi/ Got 'jkl'

Replies are listed 'Best First'.
Re^8: Understanding the benefit of Given/When ...
by LanX (Saint) on Mar 04, 2010 at 17:41 UTC
    What I meant was that phrase in the docs to for/when:

    On exit from the when  block, there is an implicit next.

    so you can use a simple if where you wanted to write  when () { ...; continue} and vice versa you can write when where you wanted to write if () {...; next} ! (as long you don't need the implicit smart match)

    In other words: This next in your code

    ... say " After when /abc/"; when (/def/) {say " Got 'def'"; next;} say " After when /def/"; ...
    is a tautology...

    Cheers Rolf

      Yes, showing there was no difference between having and not having a trailing 'next' was the reason for including that when block.