×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Passing a filehandle to a sub-routine

Passing a filehandle to a sub-routine

Passing a filehandle to a sub-routine

(OP)
Just to check, I am doing this correctly?

my $fh = open(INF, "afile.txt") or die;

my output = &read_from_file(\*INF);

sub read_from_file()
{
    while(<$_[0]>)
    {
    etc......


Cheers
Loon

RE: Passing a filehandle to a sub-routine

Hi Loon,

The script below works, it prints itself. The only difference is that I've used $h=shift and <$h> instead of just <$_[0]> which I couldn't get to work....


#!/usr/bin/perl
 
my $fh = open(INF, $0) or die;
 
print &read_from_file(\*INF);
 
sub read_from_file()
{
my $l;
my $h = shift;
 
        while(<$h>) {
            $l = $l . $_;
        }
        $l;
}

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: Passing a filehandle to a sub-routine

Why not just use the filename as the function parameter?

eg.

#!/usr/bin/perl
 
print &read_from_file($filename);
 
sub read_from_file()
{
  my ($filename) = @_;
  open(INF, $filename) or die;
  @INF = <INF>;
  close(INF);
  return @INF;
}

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Passing a filehandle to a sub-routine

Hi Tom,

Yep - passing a filename works fine - just that it's sometimes nice to pass a reference to an already open file.

For instance - working your way through an input file like this

while(<F>){
    ProcessHeaders(\F);
    ProcessDetailLines(\F);
    ProcessFooterLines(\F);
}

this way you can work your way through an open file and make the logic of the way your script works a bit more transparent.

And "Hi" by the way - nice to see you on TT

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close