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