http://www.perlmonks.org?node_id=305217

mandog has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to pass file handles to subs by value. The FAQ gave me a recipe but the position of the file gets changed.

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