From 25b991bf8caa94f23a64f9568f5ceee69781aa25 Mon Sep 17 00:00:00 2001 From: Vincent Pit Date: Sun, 3 Jan 2010 18:22:38 +0100 Subject: Make given() statements return the last evaluated expression --- pod/perlsyn.pod | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'pod') diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index 4e1bc0a8a7..f90b8b376e 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -667,6 +667,42 @@ case to the next: default { say '$foo does not contain a y' } } +=head3 Return value + +When a C statement is also a valid expression (e.g. +when it's the last statement of a block), it returns : + +=over 4 + +=item * + +An empty list as soon as an explicit C is encountered. + +=item * + +The value of the last evaluated expression of the successful +C/C clause, if there's one. + +=item * + +The value of the last evaluated expression of the C block if no +condition was true. + +=back + +Note that, unlike C and C, both C and C always +themselves return an empty list. + + my $price = do { given ($item) { + when ([ 'pear', 'apple' ]) { 1 } + break when 'vote'; # My vote cannot be bought + 1e10 when /Mona Lisa/; + 'unknown'; + } }; + +C blocks can't currently be used as proper expressions. This +may be addressed in a future version of perl. + =head3 Switching in a loop Instead of using C, you can use a C loop. -- cgit v1.2.1