my @deviceNames = do { open (my $FH, "<", $file) or die "Error: couldn't read '$file': $!\n"; # split up lines by \nAction: within the do-block local $/ = "\nAction:"; my @deviceNames = (); while (my $block = <$FH>) { # extract the last line my $deviceName = ( split(/\n/, $block) )[-1]; push (@deviceNames, $deviceName) if $deviceName; } # while # well, the following line is not really needed, because # $FH is automatically closed at the end of the do-block close ($FH); @deviceNames; } ; # do