MichaelORourke has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Perlites, I've just been involved with making a PAC (point-n-click) person happy with and 'older' version which had the peculiar- ity of the non-comma (newer) and the comma needed when dealing with an anything, but mostly for readdir ala:
my @good = grep(s/../../, readdir(DIR)); or my @better = grep({-f "./$_", readdir(DIR));
Newer Perls since 5.6.x don't need, nor do they tolerate the comma after the block I can say that I've seen folks try to use 'grep' as it might be in an Unix environ. Not so. And since I didn't use map so much for these maybe that's a way around. Nonetheless, I'll offer something soon if need be. This also goes to the wantarray construct and the man pages for same. I've already written and commented on this but for clarity -- stop moving the data around!
my $ar = \@somearray; my @array = @somearray; # from a function my $ar = &fsome(); my @array = &fsome(); sub fsome { my @interesting - qw( one two buckle my shoe ); wantarray? @interesting: \@interesting; }
Okay, let me know and thanks. morourke@theworld.com

Replies are listed 'Best First'.
Re: The way around < 5.6.x grep(... [,]* readdir(DIR)
by kyle (Abbot) on Mar 19, 2008 at 21:13 UTC

    I'm sorry, I can't quite tell what your question is.

    As for the syntax for grep, you can write a simple one two ways.

    The first is with a block and without a comma.

    grep { /x/ } @source;

    The second is with an expression and with a comma.

    grep( /x/, @source );

    The second one can be written without the parentheses, but not without the comma.

    grep /x/, @source;

    If you need to be more complicated than a simple expression, you need a block because you can't use a semicolon in a simple expression.

    grep { my $x = $_; $x =~ s/\D//g; my $y = f( $x ); ( $y > $x ) } @source;

    Sometimes you can cook a bit with operators.

    grep s/\D//g && f( $_ ) > $_, @source;

    ...but not always. The difference between the last two examples is that the second one modifies @source, while the second one doesn't.

    Again, I can't tell what your question is, so I don't know if it helps. Hopefully there's something useful.

      I also dispute the OP's implication that Perl 5.6 and/or prior (or is it "since"?) required a comma after a block argument to grep. I'm pretty sure that this particular aspect of the grammar has not changed in a very long time (not since the ability to use a block was implemented).

      - tye        

Re: The way around < 5.6.x grep(... [,]* readdir(DIR)
by parv (Parson) on Mar 20, 2008 at 03:24 UTC
    my @better = grep({-f "./$_", readdir(DIR)); # ^ ^ # ^ ^
    Do you actually have } missing, or is that just a typo?