$ cat pm_1077685_A.txt Line 1: no plate info Line 2: ... PlateID 123 ... Line 3: ... PlateID 456 ... Line 4: no plate info $ cat pm_1077685_B.txt Line 1: no plate info Line 2: ... PlateID 789 ... Line 3: ... PlateID ... Line 4: no plate info $ cat pm_1077685_C.txt Line 1: no plate info Line 2: ... PlateID 000 ... Line 4: no plate info #### #!/usr/bin/env perl use strict; use warnings; use autodie; my @filenames = qw{pm_1077685_A.txt pm_1077685_B.txt pm_1077685_C.txt}; for my $filename (@filenames) { my $in_path = "./$filename"; print "Processing: '$in_path'\n"; open my $in_fh, '<', $in_path; while (<$in_fh>) { print "\t$_"; if (/PlateID/) { if (/PlateID (\d+)/) { my $plate_id = $1; print "\t\tProcess PlateID '$plate_id' from '$in_path'\n"; } else { print "\t\tpopup_error_window($in_path)\n"; last; } } } } #### $ pm_example.pl Processing: './pm_1077685_A.txt' Line 1: no plate info Line 2: ... PlateID 123 ... Process PlateID '123' from './pm_1077685_A.txt' Line 3: ... PlateID 456 ... Process PlateID '456' from './pm_1077685_A.txt' Line 4: no plate info Processing: './pm_1077685_B.txt' Line 1: no plate info Line 2: ... PlateID 789 ... Process PlateID '789' from './pm_1077685_B.txt' Line 3: ... PlateID ... popup_error_window(./pm_1077685_B.txt) Processing: './pm_1077685_C.txt' Line 1: no plate info Line 2: ... PlateID 000 ... Process PlateID '000' from './pm_1077685_C.txt' Line 4: no plate info