use warnings; use strict; use Cwd qw(abs_path); die "Please give the directory to the file on CLI" unless @ARGV == 2; my ( $dir, $file_to_reference ) = @ARGV; my @ds; open my $fh, '<', $file_to_reference or die "can't open file: $!"; while (<$fh>) { chomp; push @ds, $_; } close $fh or die "can't close file: $!"; $dir = abs_path($dir); chdir $dir or die "No such directory: $!"; my @result; opendir my $dh, $dir or die "can't open dir: $!"; while ( my $file = readdir $dh ) { for (@ds) { push @result, $_ if $_ eq $file; } } closedir $dh or die "can't close directory: $!"; print $_, $/ for @result;