sub test{ my $handle = shift; while (<$handle>){ ..... }; print {$handle} "test\n"; } open (FH, "<file"); flock(FH, 1); test(*FH);