Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: read files one by one in directory and redirct output to a file

by mr_mischief (Monsignor)
on Jul 06, 2018 at 20:16 UTC ( #1218070=note: print w/replies, xml ) Need Help??


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';

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1218070]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2020-10-01 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (174 votes). Check out past polls.

    Notices?