Discuss Perl file handles in the Perl Programming forum on Dev Shed. Perl file handles Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
Posts: 3
Time spent in forums: 20 m 52 sec
Reputation Power: 0
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?
Code:
use FileHandle;
my $file="a_file";
my $fh = FileHandle->new("$file","r");
while(my $line =<fh>){
if ($line=~m/<a_string>/){
<some processing>
print $line;
}
}
&sub_routine_A(\$fh);
sub_routine_A {
my $fh = shift;
while(my $line = <fh>){
if($line=~m/<a_string>/){
print $line;
}
}
Posts: 513
Time spent in forums: 4 Days 20 h 27 m 52 sec
Reputation Power: 405
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.
Posts: 3
Time spent in forums: 20 m 52 sec
Reputation Power: 0
Quote:
Originally Posted by Laurent_R
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?
Posts: 3
Time spent in forums: 20 m 52 sec
Reputation Power: 0
Quote:
Originally Posted by Laurent_R
Or you close the file and re-open it. But what sense does it make to read through the whole file twice?
I need to parse the file, and the checks to be carried out are dependent on hardware platform, so i decided to devise a subroutine for each task. So that i can call them separately.
Posts: 1,653
Time spent in forums: 1 Month 2 Weeks 2 Days 6 h 26 m 34 sec
Reputation Power: 1170
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.
Last edited by FishMonger : October 13th, 2012 at 01:25 PM.