in reply to Parsing files in a directory using a list.
You already are on the right path ( strict and warnigns are there ) but maybe you need to use some idioms more: firstly the use of the better form to open a file: lexical filehandle, checking if all was good: open my $h, '<', $file or die "cannot read from $file!"
Another common way, idiomatic way of Perl thinking is: uniqueness, quantity or existence of something lead immediately to use an hash. You read a list populating the hash with each item and later on you'll be able to check against this list as simply as: .. if exists $files{$current_element}
But given your numbers (300 files is not a big deal nowadays..) you can also do all the work directly while reading the list file:
.. my $list_file = '/path/to/list.txt'; open my $read, '<', $list_file or die "cannot read from'$list_file'!"; while (<$read>){ # it populates $_ is the same of: defined $_ = <$rea +d> chomp; my $ret = process ($_) if -e "./$_"; if ($ret){print "$_ succesfully processed\n"} else{print "Warning: '$_' was not processed!\n"} } sub process{ my $file = shift; open ... or return 0 # read and do something close .. # implicitly return the last thing: if close succeded is 1 }
L*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing files in a directory using a list.
by Lotus1 (Vicar) on Apr 08, 2018 at 23:44 UTC |