in reply to Newbie: parentheses, map, etc.

my @output = map s/(.*)/    $1\n/, @small_files;

The substitution operator s returns the number of captured substrings, which is 1. It also alters the string it is bound to ($_ by default, as you are aware), but this alteration is by side-effect. Check the contents of @small_files immediately after this statement.

What you want is

my @output = map "    $_\n", @small_files;

Updated: I am taking the liberty of deleting the false part of my original answer (which is accurately quoted and accurately corrected in kyle's reply below). Thanks, kyle. What was I thinking?

Replies are listed 'Best First'.
Re^2: Newbie: parentheses, map, etc.
by kyle (Abbot) on Mar 04, 2008 at 03:47 UTC

    The substitution operator s returns the list of captured substrings or, in scalar context, the number of captured substrings, which is 1.

    This isn't true. s/// always returns the number of substitutions made or the empty string if no substitutions were made (see perlop). The expression in map is in list context.

      The documentation lies. On failure, it returns the canonical false value, which is an empty string in string context and 0 in numeric context.

        Upon first reading this, I thought, "what's the difference?" More to the point, "how does one behave differently from the other?" After all, an empty string in a numeric context is zero anyway. How would one ever see the difference?

        Some time later, the answer popped into my head. A string turned number will trigger warnings, but s/// won't. I verified this in the Test::More fashion.

        Thanks for the food for thought.