Processing the file handle in a subroutine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sangith
    New Member
    • Jun 2007
    • 25

    Processing the file handle in a subroutine

    Hi,

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

    [CODE=perl]
    open FH1, "<outfile" or die "cannot open the file for reading: $!\n";
    while ($line_from_out file = <FH1>) {
    chomp $line_from_outf ile;
    print $line_from_outf ile;
    }
    close FH1;
    [/CODE]

    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.

    [CODE=perl]
    open FH1, "<outfile" or die "cannot open the file for reading: $!\n";
    printdevicelist (*FH1);
    close FH1;

    ####SUBROUTINE# ###
    sub printdevicelist {
    my $devices = shift;
    .
    .
    .
    }
    [/CODE]

    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
    Last edited by miller; Jul 3 '07, 06:56 PM. Reason: Code Tag and ReFormatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Observe this script:

    [CODE=perl]
    use Cwd qw(abs_path);

    use strict;

    my $script = abs_path($0);

    # Standard Method
    open(FH, $script) or die "Can't open $script: $!";
    while (<FH>) {
    print "Standard Method: $_";
    }
    close(FH);

    # Passing Indirect File Handle
    open my $fh, '<', $script or die "Can't open $script: $!";
    indirectFH($fh) ;
    sub indirectFH {
    my $fh = shift;
    while (<$fh>) {
    print "Passing Indirect FH: $_";
    }
    }
    close($fh);

    # Passing File Glob
    open(FH, $script) or die "Can't open $script: $!";
    fileGlob(\*FH);
    sub fileGlob {
    my $fh = shift;
    while (<$fh>) {
    print "Passing File Glob: $_";
    }
    }
    close(FH);

    1;

    __END__
    [/CODE]

    - Miller
    Attached Files

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      PS

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

      [CODE=perl]
      printdevicelist ('outfile');
      [/CODE]

      - Miller

      Comment

      • sangith
        New Member
        • Jun 2007
        • 25

        #4
        Originally posted by miller
        PS

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

        [CODE=perl]
        printdevicelist ('outfile');
        [/CODE]

        - Miller

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

        -sangith

        Comment

        Working...