#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep 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;
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    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?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    You can use the seek function to reset the file pointer to the beginning.

    perldoc -f seek
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Or you close the file and re-open it. But what sense does it make to read through the whole file twice?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    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.

    Comments on this post

    • Laurent_R agrees : I have the same opinion as Fishmonger.
    Last edited by FishMonger; October 13th, 2012 at 01:25 PM.

IMN logo majestic logo threadwatch logo seochat tools logo