cinnamond has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I am working on my project where I should test my Perl program with different input files using GNU Makefile. My Perl code should read .txt files from the inputs directory, but right now it just reads only one specific file test.txt. Is it possible somehow to read all files from inputs directory and output to separate files in outputs directory? I tried to write inputs directory like this /path/to/file/*.txt but I guess it is not how it should be done.

EDIT: I am sorry, that's not how I wanted to explain my point. My Perl script should be executed with bash scripts (they are different for each case, it means they have each individual input) and then my Makefile should test all cases together.

This is my code

#!/usr/bin/perl use strict; use warnings; use Lingua::StopWords qw(getStopWords); my %found; my $src = '/home/aleksandra/programu-testavimas/1-dk/trunk/tests/input +s/test.txt'; my $des = '/home/aleksandra/programu-testavimas/1-dk/trunk/tests/outpu +ts/out.txt'; open(SRC,'<',$src) or die $!; open(DES,'>',$des) or die $!; my $stopwords = getStopWords('en'); while( my $line = <SRC>){ ++$found{$_} for grep { !$stopwords->{$_} } split /\s+/, lc $line; } print DES $_, "\t\t", $found{$_}, $/ for sort keys %found; close(SRC); close(DES);

Replies are listed 'Best First'.
Re: Perl wildcards in the file paths
by tybalt89 (Monsignor) on Dec 04, 2022 at 20:09 UTC

    There is also glob which would look something like this (untested)

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11148554 use warnings; use Lingua::StopWords qw(getStopWords); my %found; my $src = '/home/aleksandra/programu-testavimas/1-dk/trunk/tests/input +s/*.txt'; my $des = '/home/aleksandra/programu-testavimas/1-dk/trunk/tests/outpu +ts/out.txt'; open(DES,'>',$des) or die $!; my $stopwords = getStopWords('en'); for my $file ( glob $src ) { open(SRC,'<',$file) or die "$! opening $file"; while( my $line = <SRC>){ ++$found{$_} for grep { !$stopwords->{$_} } split /\s+/, lc $line; } print DES $_, "\t\t", $found{$_}, $/ for sort keys %found; close(SRC); } close(DES);
Re: Perl wildcards in the file paths
by Corion (Patriarch) on Dec 04, 2022 at 19:33 UTC

    Most likely you want to use readdir (together with opendir) or glob:

    my $dir = '/home/aleksandra/programu-testavimas/1-dk/trunk/tests/input +s'; opendir my $dh, $dir or die "Couldn't read '$dir': $!"; my @files = map { "$dir/$_" } grep { $_ ne '.' and $_ ne '..' } readdir( $dh ); print "Found files:"; print "$_\n" for @files;
Re: Perl wildcards in the file paths
by Fletch (Bishop) on Dec 04, 2022 at 19:51 UTC

    Path::Tiny provides a nice API at a slightly higher level than readdir and friends.

    Edit: tweaking to put each inputs' corresponding output into a separate file.

    use Path::Tiny qw( path ); my $src_dir = path( q{/home/blah/blah/blah/yakety/smakety} ); my $out_dir = path( q{/home/blah/blah/blah/output/} ); for my $txt_file_in_src ( $src_dir->children( qr{ \.txt \z }x ) ) { open( my $infh, q{<}, $txt_file_in_src ) or die qq{Can't open '$txt_file_in_src': $!\n}; my $outfile = $out_dir->child( $txt_file_in_src->basename . q{_out.t +xt} ); open( my $outfh, q{>}, $outfile ) or die qq{Can't create '$outfile': $!\n} ); ## Alternately use Path::Tiny helper ## my $infh = $txt_file_in_src->openr; ## my $outfh = $outfile->openw; ## process source file reading from $infh writing to $outfh . . . }

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Perl wildcards in the file paths
by Anonymous Monk on Dec 05, 2022 at 16:59 UTC