sub find_all_textfiles {
my ($dir) = @_;
my $fh = new IO::File;
opendir($fh, $dir) or die "Failed to read dir '$dir' ($!)\n";
my @files = readdir($fh);
closedir $fh;
my @found = ( );
foreach my $fname (@files) {
next if $fname eq '.' or $fname eq '..';
my $path = "$dir/$fname";
if (-f $path and $path =~ /[.]txt$/) {
push @found, $path;
} elsif (-d $path) {
push @found, find_all_textfiles($path);
}
}
return @found;
}
####
use strict;
use warnings;
use Data::Dumper;
use IO::File;
use Term::ReadKey;
use Time::HiRes qw(time);
$| = 1;
my @files = find_all_textfiles(".");
my $a_text = read_files(@files);
printf "Results of reading [@files] => %s\n", Dumper($a_text);
sub read_files {
my (@files) = @_;
my $a_text = [ ];
foreach my $fname (@files) {
print "Reading '$fname' ...\n";
my $fh = new IO::File($fname) or die "Can't read '$fname' ($!)\n";
chomp(my @text = <$fh>);
push @$a_text, [ @text ];
}
return $a_text;
}
##
##
use strict;
use warnings;
use Data::Dumper;
use IO::File;
use Term::ReadKey;
use Time::HiRes qw(time);
$| = 1;
my @files = find_all_textfiles(".");
my @text = read_files(@files);
printf "Results of reading [@files] => %s\n", Dumper([ @text ]);
sub read_files {
my (@files) = @_;
my @text = ( );
foreach my $fname (@files) {
print "Reading '$fname' ...\n";
my $fh = new IO::File($fname) or die "Can't read '$fname' ($!)\n";
chomp(my @lines = <$fh>);
push @text, @lines;
}
return @text;
}
##
##
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;
}
##
##
sub process_text {
my ($a_text) = @_;
while (1) {
my $key;
my $wait_until = time + 3;
while ( time < $wait_until ) {
ReadMode 3;
$key = ReadKey( -1 );
if ( defined $key ) {
print "keystroke $key\t";
}
ReadMode 0;
}
print_some_random_text($a_text);
}
}
sub print_some_random_text {
my ($a_text) = @_;
# Replacing the next line with meaningful code is left as
# an exercise for the OP.
##
print "[Debug]\n";
}