develooper Front page | perl.perl5.porters | Postings from May 2009

Re: feature 'switch' as an expression?

Thread Previous | Thread Next
From:
Rafael Garcia-Suarez
Date:
May 3, 2009 08:40
Subject:
Re: feature 'switch' as an expression?
Message ID:
b77c1dce0905030839x46629eeduf4e92a0946851b4e@mail.gmail.com
2009/4/29 Hakim Cassimally <hakim.cassimally@gmail.com>:
> I wanted to refactor some code to use 5.10.0's switch as an expression.
> That is, given that I want to set $x based on some criteria, instead of writing:
>
>    my $x;
>    given ($blah) {
>        ....
>        default { $x = 'foo' }
>    }
>
> I wanted to be able to write:
>
>    my $x = given ($blah) {
>        ....
>        default { 'foo' }
>       }
>
> Of course that's a syntax error.  Undaunted I tried wrapping it in a
> do { } block...
>
>    my $x = do {
>        given ($blah) {
>                ....
>                default { 'foo' }
>            }
>        };
>
> While that compiles, the (genuinely useful) void context warnings
> immediately signal that something is wrong.  The given block has no
> return value.  This isn't documented in perlsyn (except by the
> implicit use of the phrase "Switch statement" and the fact that it
> doesn't mention the e-word "expression"...)

I think that it's a good idea to make given return the value of the last
statement executed.

I would welcome a new test file with comprehensive tests for that,
for a start (*).

I believe that this new behaviour would be easy to implement by fiddling
with the functions pp_leavegiven and pp_leavewhen in pp_ctl.c. Looking
at the other pp_leave* functions might help.

-- 
(*) et plus si affinités

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About