in reply to RFC: The lightning-rod operator
1) you are proposing two "safe" operators in one, the first to avoid magic (autovivification at hash access) the second to introduce magic (ignore error of missing method).
I don't find this consistent.
2) The number of edge cases for the large amount of operators makes Perl hard to memorize, we are not only running out of keyboard characters, but learning Perl becomes increasingly complicated.
3) DWIM shouldn't violate orthogonality, i.e. the ability to understand the syntax logically by composing smaller concepts to a consistent whole.
If P5P wants to extent syntax, than a better support for autoboxing would be the way I'd favour for efficiency and flexibility!
->use strict; use warnings; use Data::Dump qw/dd/; my $get = sub { my ($ref ,@path)=@_; for my $member (@path){ return undef unless exists $ref->{$member}; $ref = $ref->{$member}; } return $ref; }; my %hash = ( a=> {b=> {c=> 666} }); dd \%hash; dd $hash{a}->$get(qw/b c/); dd $hash{a}->$get(qw/x x x/); # no autovivification #dd $hash{a}{x}{x}{x}; # autovivification dd \%hash;
{ a => { b => { c => 666 } } } 666 undef { a => { b => { c => 666 } } }
Please note that:
a) the name of the method is (or can be) self explanatory ( get is just a guess)
b) this approach is backward compatible, the Perl implementation might be slow but will always work as a fall back
c) performance could be easily improved by XS code in an intermediate step
d) the solution is generic, i.e. any other "missing" operator could be implemented/experimented with and hence orthogonal
e) any new syntax like ~> or whatever could be additionally implemented with a clearly named twin autobox method
Sorry if I didn't read your hole post and didn't supply an example implementation for $call but a flue with fever limits my attention span ATM ;-)
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!
PS: As a side note, I was first thinking of realizing something like a $safe-method to handle both cases
$name = $user[$idx] -> {'name'}; # may create a hash $name = $user[$idx] -> $safe -> {'name'}; # leaves @user alone $name = $person -> name; # chokes on undef $name = $person -> $safe -> name; # passes undef on
But this would not only imply too much slow magic like a proxy class but also mangle two different concepts in one operator (see point 1)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: RFC: The lightning-rod operator
by martin (Friar) on Jan 23, 2016 at 23:54 UTC | |
by LanX (Saint) on Jan 24, 2016 at 21:43 UTC | |
by martin (Friar) on Jan 30, 2016 at 17:09 UTC |