my $tag; while () { if (m//) { $tag = $1; last; # assuming there is only one tag in each file } } close ; # proceed to call the tag handler