473,978 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Processing the file handle in a subroutine

25 New Member
Hi,

I have question on processing the file handle in a subroutine.
Here is my program without subroutine:

Expand|Select|Wrap|Line Numbers
  1. open FH1, "<outfile" or die "cannot open the file for reading: $!\n";
  2. while ($line_from_outfile = <FH1>) {
  3.     chomp $line_from_outfile;
  4.     print $line_from_outfile;
  5. }
  6. close FH1;
  7.  
I am trying to write the above program as a subroutine. So I am passing the file handle as a parameter to the subroutine. I know they are stored in the default @_ array.
But how do I process the file handle and print each line by line to STDOUT.

Expand|Select|Wrap|Line Numbers
  1. open FH1, "<outfile" or die "cannot open the file for reading: $!\n";
  2. printdevicelist(*FH1);
  3. close FH1;
  4.  
  5. ####SUBROUTINE####
  6. sub printdevicelist {
  7.     my $devices = shift;
  8. .
  9. .
  10. .
  11. }
  12.  
I would appreciate if someone can help me here. If the contents of the file are stored in the scalar variable $devices, how is it it possible for me to print line by line???

I would appreciate your response!

Thanks,
sangith
Jul 3 '07 #1
3 13645
miller
1,089 Recognized Expert Top Contributor
Observe this script:

Expand|Select|Wrap|Line Numbers
  1. use Cwd qw(abs_path);
  2.  
  3. use strict;
  4.  
  5. my $script = abs_path($0);
  6.  
  7. # Standard Method
  8. open(FH, $script) or die "Can't open $script: $!";
  9. while (<FH>) {
  10.     print "Standard Method: $_";
  11. }
  12. close(FH);
  13.  
  14. # Passing Indirect File Handle
  15. open my $fh, '<', $script or die "Can't open $script: $!";
  16. indirectFH($fh);
  17. sub indirectFH {
  18.     my $fh = shift;
  19.     while (<$fh>) {
  20.         print "Passing Indirect FH: $_";
  21.     }
  22. }
  23. close($fh);
  24.  
  25. # Passing File Glob
  26. open(FH, $script) or die "Can't open $script: $!";
  27. fileGlob(\*FH);
  28. sub fileGlob {
  29.     my $fh = shift;
  30.     while (<$fh>) {
  31.         print "Passing File Glob: $_";
  32.     }
  33. }
  34. close(FH);
  35.  
  36. 1;
  37.  
  38. __END__
  39.  
- Miller
Attached Files
File Type: txt scratch.txt (639 Bytes, 522 views)
Jul 3 '07 #2
miller
1,089 Recognized Expert Top Contributor
PS

A more sensible solution would be to simply let the subroutine handle the file operations:

Expand|Select|Wrap|Line Numbers
  1. printdevicelist('outfile');
  2.  
- Miller
Jul 3 '07 #3
sangith
25 New Member
PS

A more sensible solution would be to simply let the subroutine handle the file operations:

Expand|Select|Wrap|Line Numbers
  1. printdevicelist('outfile');
  2.  
- Miller

Thank you very much for your response! I totally understand.

-sangith
Jul 3 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1588
by: Anders Søndergaard | last post by:
Hi, I'm trying to process a large filesystem (+20 million files) and keep the directories along with summarized information about the files (sizes, modification times, newest file and the like) in an instance hierarchy in memory. I read the information from a Berkeley Database. I'm keeping it in a Left-Child-Right-Sibling instance structure, that I operate on recursively.
8
1596
by: changereality | last post by:
I am trying to process raw IIS log files and insert them into a MySQL database. I have no problem accomplishing this, but the php code runs very slow. Right now, it is processing 10,000 lines in a log file every ~300 seconds. I am dealing with daily log files with records over 500,000. It takes hours to process a daily file. <?php error_reporting(E_ALL);
6
2121
by: soren juhu | last post by:
Hi, I am developing a C Program for reading over a million files of size 1 kilobytes each and sending the contents to another program using some middle ware. I need some help on designing the program to process such a large number of files in less than 8 hours. TIA Soren
0
1874
by: JR | last post by:
Hello, Although I consider myself a proficient C++ programmer (and am getting better everyday at C# :-) I am new to and having a little trouble with some of the concepts behind ASP and ASP.NET. Particularly regarding the streaming of data between IE client and webserver. Here is what I am attempting to do.
6
5021
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
4
3635
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to scan very large ASCII files (80GB+), compare adjacent lines, and conditionally do some further processing. I believe the disk i/o is the main bottleneck so for now that's what I'm optimizing. What I have now is roughly as follows (on python...
2
7221
by: Louis | last post by:
Hope somebody can tell me why this happens to filehandles... I am playing with perl packages, and I had all functions in one file. Now I want to split them into different files using package. BEFORE: I have a function that opens a filehandle (FH1) and then calls another function to print messages. This works good. NOW: I put the calling function in another file. When it calls the print function it uses &main::prtmess();, perl prints...
1
1485
by: dgr7 | last post by:
hello, I have a file BLURG.txt in a subdirectory D:\B HC\NewB that I'd like to, using a VB6 subroutine, pass as a variable to the subroutine the name of the file & directory where it can be found (for this ex., D:\B HC\NewB\BLURG.txt), then the subroutine will connect to a FTP site, ftp://192.782.32.1 using a username of
1
3893
by: Steveaux | last post by:
Hi, I'm new to ASP.Net, so this may be something simple that I forgot. I have a form where a person can create a login. I'm doing the processing on this myself. The form has a plethora of validators, including two custom validators. They check whether the user name exists among the users already registered, and the same with the email address. Both the validators process, but they don't stop the form from processing, so I end up with...
0
10175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11592
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8465
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5162
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4742
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3773
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.