Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 13th, 2012, 04:32 AM
chuck_norris chuck_norris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 chuck_norris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}
}

Reply With Quote
  #2  
Old October 13th, 2012, 07:19 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
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.

Reply With Quote
  #3  
Old October 13th, 2012, 07:37 AM
chuck_norris chuck_norris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 chuck_norris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old October 13th, 2012, 10:07 AM
FishMonger FishMonger is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 6 h 26 m 34 sec
Reputation Power: 1170
You can use the seek function to reset the file pointer to the beginning.

perldoc -f seek

Reply With Quote
  #5  
Old October 13th, 2012, 12:45 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 513 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Days 20 h 27 m 52 sec
Reputation Power: 405
Or you close the file and re-open it. But what sense does it make to read through the whole file twice?

Reply With Quote
  #6  
Old October 13th, 2012, 12:50 PM
chuck_norris chuck_norris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 3 chuck_norris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old October 13th, 2012, 01:22 PM
FishMonger FishMonger is online now
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,653 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
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.
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Perl file handles

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap