http://qs1969.pair.com?node_id=597057


in reply to search for a pattern in file without opening the file

Well, you need to search through the files' contents instead of searching through the files' names. For example by using the external grep utility or by actually opening the file, reading its contents and looking for the stuff you want. There is no way to find out about the contents of a file without opening it and reading it.

use strict; sub contains_pattern { my ($file,$pattern) = @_; open my $fh, "<", $file or die "Couldn't read '$file': $!"; grep { /$pattern/ } <$fh>; }; my @files = grep { contains_pattern $_, qr/^source*/ } @arr1;

I also notice that you use /^source*/ as your search pattern. This most likely doesn't do what you want unless you want to match all strings starting with sourc (no e).

Update: Added the part about opening the file