Perl file handles

Hi,

Hi,

Is there a way, in perl, to pass a file handle to multiple subroutines while still using the same file handle in the 'main' section of the program.

this does not appear to be working properly.
If i place the call to the subroutine above the while in 'main'
i get the output from the subroutine, if i place it after, i get the lines matching 'a_string'. Is there a way to get both?

I think that your problem is that, in your code as it is shown above, the while loop in the "main" section will read the whole file through to the end, and the while loop in the subroutine will have nothing to read, because the file handler will be at the end of the file.

Now, if you call the subroutine first, it will be the opposite: the while loop in the subroutine will read the whole file, and the loop in the main section will have nothing to read.

I think that your problem is that, in your code as it is shown above, the while loop in the "main" section will read the whole file through to the end, and the while loop in the subroutine will have nothing to read, because the file handler will be at the end of the file.

Now, if you call the subroutine first, it will be the opposite: the while loop in the subroutine will read the whole file, and the loop in the main section will have nothing to read.

Ok, so is there a way to 'reset' the file handle to point to the beginning of the file?

It's normally best to parse the file only once and preform the different tasks as you go. Keep in mind that I/O is typically one on the slowest aspects of a script and the more you can reduce it the more efficient your script becomes.

If that doesn't work in your case, you could load the data into an array any loop over it as many times as needed.

If you provide us with more details of what you're needing to accomplish and your actual code, we would be in a better position to give more precise recommendations.