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';
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.