in reply to Writing unix-style filters

I think your solution is correct, but your actual implementation could be more succinct.

This:

@files = map { -f $_ ? $_ : () } @ARGV;
could (and arguably should) be written as
@files = grep { -f $_ } @ARGV;

This:

while ( <> ) { chomp; push @files, $_; }
could be written as
chomp( @files = <> );
though you might have a concern if the file list is huge, relative to your available memory. :-)

Ultimately, the whole chunk of code could be written as

chomp( @files = @ARGV ? grep { -f $_ } @ARGV : <> );

A word spoken in Mind will reach its own level, in the objective world, by its own weight

Replies are listed 'Best First'.
Re^2: Writing unix-style filters
by bart (Canon) on Apr 11, 2007 at 11:18 UTC
     chomp( @files = @ARGV ? grep { -f $_ } @ARGV : <> );
    This chomps after you do the file test, with the newline not yet removed. So, it will just produce an empty array, when reading the file list from the input, because -f $_ will always return false.

      No; actually, it will not, because the -f test operator here is not being applied to the strings which are returned from the <>. Perhaps it should be; in which case you'd have a very good point.

      chomp( @files = @ARGV ? @ARGV : <> ); @files = grep { -f $_ } @files;
      A word spoken in Mind will reach its own level, in the objective world, by its own weight