#!/usr/bin/perl use strict; use warnings; use IO::All; use Data::Dumper; foreach my $file (@ARGV) { if ( -e $file ) { # double check existence my @lines = io($file)->chomp->slurp; # Chomp as you slurp if (my @matches = grep( /string/, @lines ) ) { print "Found in File: '$file':\n"; print Dumper \@matches; } } } __END__ $ perl test.pl a.txt b.txt c.txt Found in File: 'a.txt': $VAR1 = [ 'Line two interested, string' ]; Found in File: 'b.txt': $VAR1 = [ 'Line one interested, string', 'Line two interested, string' ]; __DATA__ $ cat a.txt b.txt c.txt Line one not interested Line two interested, string Line one interested, string Line two interested, string Line one not interested Line two not interested Line three not interested