in reply to Re^2: RFC: The lightning-rod operator
in thread RFC: The lightning-rod operator
I find the desired short cut behaviour to catch undef even more problematic than the rest.
I'd rather prefer a catch_undef { BLOCK } command, because the block would be explicit about what is caught without much explanation.
This can be emulated (at least) with
my $h_b = {}; my $x = eval { use warnings FATAL => 'uninitialized' ; $h_b->{velocity}." mph"; } // 'unknown'; print $x;
I tried to construct some syntactic sugar
sub catch_undef (&) { my $code_ref = shift; eval { use warnings FATAL => 'uninitialized' ; $code_ref->() }; }
but I'm running into two problems:
1) Obviously pragmas are lexically scoped, I seem to remember there are some obscure tricks to manipulate the warning flags of a coderef (something with $^H ?) but I'm too lazy at the moment.
2) seems to be a bug in the parser, because I get a weird syntax error for
catch_undef { $ref->{velocity} .'mph' } // "unknown";
Too many arguments for main::catch_undef at /tmp/tst.pl line 29, near "// "unknown""
using || instead solves the parsing problem (but not the task)
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: RFC: The lightning-rod operator
by martin (Friar) on Jan 30, 2016 at 17:09 UTC |