There is a lot of documentation on looping through files on PERL, but I am running into an issue I haven't found a solution to. I haven't been able to get any of the examples for looping through lines in text files working yet.
First, my environment: I'm on a MacBook using macOS Sierra version 10.12.6. I'm writing my scripts in and running them from TextWrangler version 5.5.2.
I would like to write the sub ProcessFile in the below script to do string parsing on each line of the text files in my directory, but this is not running at all. There is no output in TextWrangler's Unix Script Output.log.
#!/usr/bin/env perl
use strict;
use warnings;
my $dirName = "/Users/me/Documents/examples";
chdir $dirName or die "Cannot chdir $dirName: $!";
opendir ( my $dir, $dirName ) or die "Cannot open directory $dirName: $!";
my @files = readdir $dir;
# Put in variable for debugging with less than the full directory
#my $numberOfFiles = scalar( @files );
my $numberOfFiles = 10;
for ( my $iFiles = 0; $iFiles < $numberOfFiles; $iFiles++ )
{
# Check if .txt file
if ( $files[$iFiles]=~/\.txt$/ )
{
my $fileName = "$files[$iFiles]";
ProcessFile ( $fileName );
}
}
closedir $dir;
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
while (my $line = <$inputFile>)
{
print "$line";
#Add parsing to gather metrics on the number of instances of different patterns
}
}
However, if I change ProcessFile to the below it prints the first 2 lines of the file:
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
my $line = <$inputFile>;
print "$line";
my $line2 = <$inputFile>;
print "$line2";
}
Also, if I change ProcessFile to the below it prints a line for each line in the file:
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
my $i = 0;
while (my $line = <$inputFile>)
{
print "$i\n";
$i++;
}
}
At this point I'm not sure what the next step should be to be able to iterate through each line of the file and parse it as a string. Eventually I'm going to want to output matching strings to a file as well, but I need to pass this hurdle first.
It must be editor/IDE issue. Your original code works fine. I created the directory with 3 txt files, ran your code and here is the output:
[18:04:40][filip@lap2:~]$ more /Users/me/Documents/examples/*
::::::::::::::
/Users/me/Documents/examples/a.txt
::::::::::::::
a
a
a
::::::::::::::
/Users/me/Documents/examples/b.txt
::::::::::::::
b
b
b
::::::::::::::
/Users/me/Documents/examples/c.txt
::::::::::::::
c
c
c
[18:04:48][filip@lap2:~]$ perl 49779605.pl
b.txt
b
b
b
c.txt
c
c
c
a.txt
a
a
a
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.