in reply to Parsing files in a directory using a list.

hello oxalate and welcome to the monastery and to the wonderful world of Perl!

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*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

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

    Hi Discipulus. I noticed you used a my declaration in a conditional statement. This is something I avoid since it can cause strange and subtle bugs. Refer to this node where Haukex pointed out to me the reason with this quote from perlsyn.

    NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.