Temporary variables are clutter but worse they help habituate one to write in small phrasings. I have been trying out writing with fewer temporary variables and finding it beneficial. Avoiding them pushes one to grasp more complex phrasings.

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

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;
(Six lines of one to three chunks.)

now I am more apt to write

%h = ( map { $_, $h{$_} } sort { $a<=>$b } keys %h)[0,1];
(One line of five or seven chunks.)

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?

%h = PRE_SUB_OP[0,1]( map { $_, $h{$_} } sort { $a<=>$b } keys %h);
Having the the short clause first seems to add clarity but perhaps I am just missing a trick, or some experience.

Be well.


In reply to small steps toward Perl literacy, temp vars and parentheses by rir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.