#!/usr/bin/perl -w use strict; sub find { local *IN = shift; my $find_me = shift; my $count = 0; # duplicate existing file handle open F2, '<&', *IN or die "Can not duplicate file handle"; my $tell = tell(F2); while () { $count++ if (/$find_me/); } return "count: $count tell: $tell\n"; close F2; } open OUT, '>', 'tmp.txt' or die "$!\n"; print OUT while (); close OUT or die "$!\n"; open IN, 'tmp.txt' or die "$!\n"; print find( *IN, 'a' ); print find( *IN, 'a' ); print find( *IN, 'd' ); print find( *IN, 'e' ); __DATA__ a a a c d e