Re: A file-handle query



Prasad J Pandit wrote:
Hello there!

Hello,

I'm a novice perl programmer and I'm trying to pass a
File-Handle to two different subroutines one after the
other as follows

#!/usr/bin/perl -w
use strict;
my $FH;

$FH is a scalar variable.

open($FH, "filename") || die "Could not open file! \n";

You can declare the scalar here if you want:

open(my $FH, "filename") || die "Could not open file! \n";

func_one(\*$FH);
func_two(\*$FH);

Just use the scalar:

func_one($FH);
func_two($FH);

exit(0);

sub func_one
{
     local * FH = $_[0];

And then store it locally like:

      my $FH = $_[0];

     my $line;
     while(defined($line = <$FH>))
     {
           printf($line);
     }
}

sub func_two
{
     local * FH = $_[0];

my $FH = $_[0];

     my $line;
     while(defined($line = <$FH>))
     {
           printf($line);
     }
}


John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • A file-handle query
    ... File-Handle to two different subroutines one after the ... use strict; ... sub func_one ...
    (perl.beginners)
  • Re: use strict and filehandles
    ... Then throughout the program, different subroutines print to the log files, e.g. ... I just get an error that I can't do something with strict something-else (refs ... perldoc -q "How do I pass filehandles between subroutines" ...
    (perl.beginners)
  • Re: use strict and filehandles
    ... Then throughout the program, different subroutines print to the log files, e.g. ... in code sample A below, ERRLOG and INFLOG are declared at the start. ... I just get an error that I can't do something with strict something-else (refs ...
    (perl.beginners)
  • Re: When to "use strict" when teaching?
    ... programs are big enough to need to have subroutines. ... And 'strict vars' helps you remember to declare the ... the very worst of all possible programming practices. ...
    (comp.lang.perl.misc)
  • use strict and local variables
    ... is there a possibility to use "use strict" and to define variables in ... which are also seen in the subroutines called from this ... I want the same value in the var in the subroutine like before, ...
    (perl.beginners)