my %data;
my @FILES = @ARGV;
# or maybe @FILES = glob("some/directory/*.ext");
Since this is Perl, there are many ways to do it.
# 1. Open and load each file
foreach my $file (@FILES) {
local $/ = undef;
open my $fh, '<', $file;
$data{$file} = <$fh>;
}
# 1b. Using map
%data = map {
local $/=undef;
open my $fh, '<', $file;
my $data = <$fh>;
$_ => $data
} @FILES;
# 2. Leverage your operating system
foreach my $file (@FILES) {
$data{$file} = qx(/bin/cat "$file");
}
# 3. Use a module
use File::Slurp;
foreach my $file (@FILES) {
$data{$file} = File::Slurp::slurp($file);
}
# 3b, using map
use File::Slurp;
%data = map { $_ => File::Slurp::slurp($_) } @FILES;
No comments:
Post a Comment