use strict; use warnings; use Data::Dumper; use IO::File; use Term::ReadKey; use Time::HiRes qw(time); $| = 1; my $a_text = read_textfile_lines("."); printf "Text from ALL textfiles in current dir: %s\n", Dumper($a_text); sub read_textfile_lines { my ($dir, $a_text) = @_; $a_text ||= [ ]; my $fh = new IO::File; opendir($fh, $dir) or die "Failed to read dir '$dir' ($!)\n"; my @files = readdir($fh); closedir $fh; foreach my $fname (@files) { next if $fname eq '.' or $fname eq '..'; my $path = "$dir/$fname"; if (-f $path and $path =~ /[.]txt$/) { $fh = new IO::File($fname) or die "Can't read '$fname' ($!)\n"; chomp(my @lines = <$fh>); push @$a_text, @lines; close $fh; } elsif (-d $path) { read_textfile_lines($path, $a_text); } } return $a_text; }