diff options
author | Vincent Pit <perl@profvince.com> | 2010-01-03 18:22:38 +0100 |
---|---|---|
committer | Vincent Pit <perl@profvince.com> | 2010-01-03 18:37:37 +0100 |
commit | 25b991bf8caa94f23a64f9568f5ceee69781aa25 (patch) | |
tree | 77b6a52863d1c3a9cbcc3f323ea7f873e99a3d36 /pod | |
parent | fd909433c74372968d34d9cad8f4458ab60e19b4 (diff) | |
download | perl-25b991bf8caa94f23a64f9568f5ceee69781aa25.tar.gz |
Make given() statements return the last evaluated expression
Diffstat (limited to 'pod')
-rw-r--r-- | pod/perlsyn.pod | 36 |
1 files changed, 36 insertions, 0 deletions
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<given> 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<break> is encountered. + +=item * + +The value of the last evaluated expression of the successful +C<when>/C<default> clause, if there's one. + +=item * + +The value of the last evaluated expression of the C<given> block if no +condition was true. + +=back + +Note that, unlike C<if> and C<unless>, both C<when> and C<default> 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<given> 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<given()>, you can use a C<foreach()> loop. |