in reply to Newbie: parentheses, map, etc.

It's not working because s/// returns the number of replacements that it did, not the string after replacement. If you want that, you can say map { s/.../.../; $_ } @stuff. Note, however, that this modifies the original list. Since you're modifying the values in place anyway, you might as well use a for loop instead.

s/(.*)/ $1\n/ for @small_files;

The error you get without the parentheses on grep is (at least when I tried it):

Warning: Use of "-s" without parentheses is ambiguous Unterminated <> operator

You can disambiguate -s by putting $_ in the expression explicitly.

my @small_files = grep -s $_ < 10_000, @ARGV;

Otherwise, it thinks that you were going to say something like -s <STDIN> but forgot the closing >.

Replies are listed 'Best First'.
Re^2: Newbie: parentheses, map, etc.
by nefigah (Monk) on Mar 04, 2008 at 03:57 UTC
    Otherwise, it thinks that you were going to say something like -s <STDIN> but forgot the closing >.

    Ah, okay, that makes sense.

    I also understand where I was in error with the regex solution. Funny that I could have just put "    $_\n" :) I guess I didn't think of it because when I think map, I think some sort of function call or active statement should be going on, so the basic string literal eluded me.

    So not explicitly writing the $_ is generally bad? :(

    Thanks everyone!

      I more or less agree with a rule of thumb for $_ in Perl. If you have to use $_ explicitly, it might be better to use something else. I make exceptions for map and grep, though. I pretty much always expect $_ to appear there. In this specific case, I don't think it hurts anything to stick in an explicit $_ (but I'd probably like it better without). You could achieve that, if you really want, by reversing the test: grep 10_000 > -s, @ARGV.

      All just my opinions, of course.