Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Jul 06, 2018 at 13:00 UTC ( [id://1218050]=note: print w/replies, xml ) Need Help??


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

A non-module approach (or: why do explicit loops when perl will do them for you?).

#!/usr/bin/perl # https://perlmonks.org/?node_id=1218012 use strict; use warnings; my $dirname = 'some/test'; my $outputfile = 'some.output.name'; local @ARGV = grep -f, glob "$dirname/*"; @ARGV or die "no files in $dirname"; open my $out, '>', $outputfile or die "$! opening $outputfile"; print $out $_ while <>; close $out;
  • Comment on Re: read files one by one in directory and redirct output to a file
  • Download Code

Replies are listed 'Best First'.
Re^2: read files one by one in directory and redirct output to a file
by haukex (Archbishop) on Jul 06, 2018 at 13:09 UTC
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

    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';
Re^2: read files one by one in directory and redirct output to a file
by TonyNY (Beadle) on Jul 06, 2018 at 14:45 UTC
    Exactly what I was looking for, Thanks Tybalt!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-16 15:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found