while ($fh) { /^Confname/ or next; work_with $_; last; ## if you want to process only the first ocurrence of such line } #### my @confnames = grep /^Confname/, <$fh>; ## if you want to process all lines: work_with $_ for @confnames; ## else: work_with $confnames[0];