use strict; use warnings; while () { my $index = index($_,'|'); next unless $index; # add error checking!! my $tag = substr($_,0,$index); expensive_function($tag,$_) if $tag =~ / /; } sub expensive_function { my ($tag,$line) = @_; my ($label,$number) = split(' ',$tag,2); my ($digit) = $label =~ /_(\d+)$/; my @field = split(/\|/,$line); # now do something with all of this ... } __DATA__ START_1 123| FILE 2222| XXXX| AAAA| NEW | END_1|