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

In reply to Re^2: read files one by one in directory and redirct output to a file by mr_mischief
in thread read files one by one in directory and redirct output to a file by TonyNY

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.