|Just another Perl shrine|
Re: desire to pass file handles by valueby Roger (Parson)
|on Nov 07, 2003 at 02:42 UTC||Need Help??|
I think you *need* to rewind the file by calling seek FH, 0, 0; in the subroutine find.
The file operation in Perl is ultimately linked to the underlying unix file handles. When you read from a Perl file handle, Perl uses and updates the underlying unix file handle, thus giving the side effect.
I think ideally you are looking for the Perl's equivalent to the C stdio's dup function, to duplicate/copy an existing file handle into a new file handle (not just an alias to the existing file handle, but a new independent file handle), and operate on the second file handle in the subroutine. I am not aware of such facility in Perl 5 (my lack of research perhaps), but I think Perl 6 has implimented the dup function for the File Object.
Ok, I just learned that you can duplicate a file handle in Perl with open NEW, "<&OLD";. I quickly came up with the following code -
The output is still -
That didn't work either! Ok, that taught me a lession - my assumption on the duplicated file handle could be wrong. I need some re-education.
Fellow monks, could you please tell me what is wrong with the duplicated file handle? Is perl actually creating a second independent file handle? Am I doing the right thing at all?