Help - Search - Member List - Calendar
Full Version: A file-handle query
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Prasad J Pandit
Hello there!
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;
open($FH, "filename") || die "Could not open file!
n";
func_one(*$FH);
func_two(*$FH);
exit(0);

sub func_one
{
local * FH = $_[0];
my $line;
while(defined($line = <$FH>))
{
printf($line);
}
}

sub func_two
{
local * FH = $_[0];
my $line;
while(defined($line = <$FH>))
{
printf($line);
}
}

Problem is first function(func_one) works fine,
while second does nothing. My guess is that
file-handle is reaching at the end of the file in
func_one and thus there remains nothing for func_two
to read in.
Could someone tell me how can I make both of
them(func_[one/two]) work without openning the file
twice in two functions?


Regards
-Prasad
PS: Please don't send me html/attachment/Fwd mails



____________________________________________________
Yahoo! Sports
Rekindle the Rivalries. Sign up for Fantasy Football
http://football.fantasysports.yahoo.com

Jeff 'japhy' Pinyan
On Jun 24, John W. Krahn said:

QUOTE
Prasad J Pandit wrote:
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

Just use the scalar:

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

At the end of reading the file, if you want to rewind it, you need to
write

seek(FILEHANDLE, 0, 0);

to get back to the beginning.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

John W. Krahn
Prasad J Pandit wrote:
QUOTE
Hello there!

Hello,

QUOTE
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.

QUOTE
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";

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

Just use the scalar:

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

QUOTE
exit(0);

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

And then store it locally like:

my $FH = $_[0];

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

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

my $FH = $_[0];

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


John
--
use Perl;
program
fulfillment


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.