I'll take the first one, but I haven't played with forks so I'll leave that to wiser monks.

s/\A\s+//, s/\s+\z//
You are correct that this code is stripping white space. More precisely, it is stripping leading and trailing white space. In fact, this task is performed so often there is a snippet in perlfaq4. If you are wondering about the use of \A and \z, see perlre:
The \A and \Z are just like "^" and "$", except that they won't match multiple times when the /m modifier is used, while "^" and "$" will match at every internal line boundary. To match the actual end of the string and not ignore an optional trailing newline, use \z.

The other bit:

for my @r = @_;
does a couple of things. The for causes the preceding expression (regexen) to be executed for each of the items in the following list (the array @r). The @r = @_ makes a copy of the parameters passed to the subroutine (in @_, see perlvar) before they are used. This is important because the values in @_ are aliases to the original values (in the calling code, see perlsub) and if a copy was not made the original values would be modified. You can test this pretty easily - just change the code to use @_ directly and see what happens to the original values

Update: There are a couple of other things worth mentioning. The way the for loop is constructed, the default variable $_ is used (see perlvar), which aliases the elements in @r. Second, the final line, which contains a solitary @r, is a shorthand way of specifying the return value of the routine (remember, the elements of @r were changed via aliasing in the for loop). See perlsub:

If no return is found and if the last statement is an expression, its value is returned.
I won't get into how the value of the expression is determined as it is context dependent and can be tricky to understand.


In reply to Re: How does this code work? (from "Perl is Unix") by bobf
in thread How does this code work? (from "Perl is Unix") by Anonymous Monk

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.