By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Processing the file handle in a subroutine

P: 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
Share this Question
Share on Google+
3 Replies


miller
Expert 100+
P: 1,089
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, 431 views)
Jul 3 '07 #2

miller
Expert 100+
P: 1,089
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

P: 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.