Hi james28909
If I could say, why not use a subroutine for repeated works. It's defined a clean and clear path of work.
Since, you are reading through several files, use a subroutine, that read files, and does all you wanted then you can use the result returned. In fact, you don't have to manually close the file, because the file is closed for you once the subroutine is done for each file opened.
Then, you can deal with each part of your work separately.
E.g Let say I want to read line 10, of each file in a particular folder:
use warnings;
use strict;
my $dirname = $ARGV[0] // '.';
for ( glob("$dirname/*") ) {
if ( !-d ) {
read_file($_);
}
}
sub read_file {
my $filename = shift;
open my $fh, '<', $filename or die "can't open file:$! ";
while (<$fh>) {
print $_ if $. == 10;
}
}
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author
unknown to me