The split and join I'm sure you understand. I suspect the bit you find interesting is the "swap without temporary" - an old assembly language trick. In situations where there weren't enough registers to use one for temporary storage when swapping two registers and the time cost of going off chip was great this trick could be used to peform the exchange. The three xors sort of slide the bits from each regiser through each other. Consider (note binary values - this is not Perl):

a = 0011 b = 1010 a = a ^ b (a now contains 1001) b = a ^ b (b now contains 0011 - a's original contents) a = a ^ b (a now contains 1010 - b's original contents)

In the example code two strings get swapped using the technique. However if the strings are different lengths then the shorter string will end up with trailing null characters. The substitution strips the nulls. A tr could have been used instead, but substitutions are more often used in general so a better thing for the OP to be made aware of.


DWIM is Perl's answer to Gödel

In reply to Re^5: How do I reverse the order of the first and last word of a string? by GrandFather
in thread How do I reverse the order of the first and last word of a string? by ferrispike12

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.