FILE_LIST: foreach my $filename ( @files ) { open FILE, "<", $filename or die "Can't open $filename. $!\n"; my $headers = 0; while ( my $line = ) { $headers++ if $line =~ /\bFULL\b/; if ( $headers > 1 ) { # do whatever it is you intend to do with # multi-header'ed files. next FILE_LIST; } } # Do whatever it is you intended to do with # single-header files. } continue { close FILE; } # Now you're done.