http://qs321.pair.com?node_id=1218070


in reply to Re: read files one by one in directory and redirct output to a file
in thread read files one by one in directory and redirct output to a file

why do explicit loops when perl will do them for you?

Well, there's more than one way to have less explicit loops. Not all of them require playing dirty with ARGV or using globs.

#!perl use strict; use warnings; my $dir = 'some/directory'; opendir( my $d, $dir ) or die "can't open dir $dir: $!\n"; open my $o, '>', './output' or die "can't write to file: $!\n"; print {$o} map { open my $in, '<', sprintf '%s/%s', $dir, $_; local $/; <$in> + } grep { -f sprintf '%s/%s', $dir, $_ } readdir $d; close $o or die "can't complete writing to file: $!\n"; closedir $d;

Such a brief representation gets a little more troublesome if one wants to handle errors rather than dying, or if one wants to warn on errors for each input file.

If one really wants to make the main program brief and high-level but have plenty of places to add checks and error handling without making the main program itself noisy, there are ways to do that too. There are still no modules necessary.

#!perl use strict; use warnings; sub read_file ($) { my $file = shift; if ( open my $in, '<', $file ) { local $/; return <$in>; } else { warn "can't read file $file: $!\n"; return (); } } sub files_from_dir ($) { my $dir = shift; opendir( my $d, $dir ) or die "can't open dir $dir: $!\n"; my @list = readdir $d; closedir $d; return map { -f (sprintf '%s/%s', $dir, $_) ? (sprintf '%s/%s', $d +ir, $_) : () } @list; } sub write_to_file ($@) { my $output = shift; open my $o, '>', $output or die "can't write to file : $!\n"; print {$o} @_; close $o or die "can't complete writing to file: $!\n"; } write_to_file './output', map { read_file $_ } files_from_dir '/some/d +irectory';