I think there's point being missed. The fact that Perl hides a temporary while doing a   ($b,$a) = ($a,$b); swap isn't important. The salient point is that there isn't a temporary that the programmer has to mentally account for. This is a wetware optimization, not a memory or processor optimization.

The other ways of swapping might work without a hidden temporary, but all of them contribute more to a programmer's cognitive load than does the simple swap.

A developer with moderate Perl skills looks at  ($b,$a) = ($a,$b); and thinks "oh, a swap", and moves on. But faced with

$a ^= $b; $b ^= $a; $a ^= $b;
the developer has to stop and walk through it. It might look like an XOR swap, but that're still three statements to grok just to make sure. Think of this as a wetware performance hit.

A big part of the development game is containing cognitive load -- yours and others.


In reply to (dws)Re: Temporary Variables by dws
in thread Temporary Variables by srawls

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.