That is the prime point of this post. I'll demonstrate it once and wander on to a few adjacent thoughts.
Where I might have written something like
(Six lines of one to three chunks.)my $low_key = (keys %h)[0]; for my $k ( keys %h ) { $low_key = $k if ($k < $low_key ); } my $val_l_k = $h{$low_key}; %h=(); $h{$low_key}=$val_l_k;
now I am more apt to write
(One line of five or seven chunks.)%h = ( map { $_, $h{$_} } sort { $a<=>$b } keys %h)[0,1];
I don't consider myself facile with Perl, I don't code enough, but I have started to feel how extraneous parentheses impede the quick grasp of code.
In the second bit of code there is a lot of contextual info to tell one to jump to, or just expect more after, the matching parenthesis. But I don't tend to do that, I keep reading to the right and find the subscripting a little bit of a surprise. So I find this is about my limit in reading code; reading as opposed to puzzling things out.
I ask those who are more Perl literate: What do you do when you get to an opening parenthesis?
If the map and sort code blocks were larger, how would you read that code, would you grasp the structure first and come back to read the code blocks?
For myself I think a construction like the below could be helpful. Did K&R C allow subscripts to precede their expressions?
Having the the short clause first seems to add clarity but perhaps I am just missing a trick, or some experience.%h = PRE_SUB_OP[0,1]( map { $_, $h{$_} } sort { $a<=>$b } keys %h);
Be well.
In reply to small steps toward Perl literacy, temp vars and parentheses by rir
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |