469,352 Members | 1,766 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Processing the file handle in a subroutine

25
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 13122
miller
1,089 Expert 1GB
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, 471 views)
Jul 3 '07 #2
miller
1,089 Expert 1GB
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
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

Post your reply

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

Similar topics

6 posts views Thread by Anders S√łndergaard | last post: by
8 posts views Thread by changereality | last post: by
6 posts views Thread by soren juhu | last post: by
reply views Thread by JR | last post: by
4 posts views Thread by Alexis Gallagher | last post: by
2 posts views Thread by Louis | last post: by
1 post views Thread by Steveaux | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.