sending file handle as function argument

P

Perl Learner

how to i pass a file handle as a function argument ?

for example i have

open(OUTFILE, ">someoutput.dat");

i want to do something like

sub function_that_writes_to_given_file_handle
{
my (??????) = @_;

}

i would appreciate it if someone could give me a direction here.

thanks.
 
A

Arne Ruhnau

Perl said:
how to i pass a file handle as a function argument ?

for example i have

open(OUTFILE, ">someoutput.dat");

i want to do something like

sub function_that_writes_to_given_file_handle
{
my (??????) = @_;

}

You can use

open my $outfile, ">someoutput.dat";

instead.

perldoc -f open

hth,

Arne Ruhnau
 
T

Tad McClellan

Perl Learner said:
how to i pass a file handle as a function argument ?


The way the Perl FAQ says to.

perldoc -q filehandle


How can I make a filehandle local to a subroutine? How do I
pass filehandles between subroutines? How do I make an array
of filehandles?

i would appreciate it if someone could give me a direction here.


You are expected to check the Perl FAQ *before* posting to
the Perl newsgroup.
 
O

optional

File handles are in global scope. You can open them in one function
and use them in a different function or main.
 
A

A. Sinan Unur

[ Please quote some context when replying. ]
File handles are in global scope. You can open them in one function
and use them in a different function or main.

Just because you *can* do something doesn't mean that you should do it.

Why would you want to cause coupling between the name you choose for
your the file handle, and all of the subroutines that use it. If you
change the name of the file handle in one place, then you have to go
find the other N places where it also needs to be changed.

So, if you have an alternative to bareword file handles, you use it:

open my $input_fh, '<', $filename
or die "Cannot open $filename: $!";

mysub($input_fh);

etc ...

On the other hand, if you do not have lexical file handles (because you
have to cater to much older versions of Perl), you can still do:

#! /usr/bin/perl

use strict;
use warnings;

open FILE1, '>', 'file1' or die "Cannot open file1: $!";
open FILE2, '>', 'file2' or die "Cannot open file2: $!";

hello ($_) for (*FILE1, *FILE2);
goodbye($_) for (*FILE1, *FILE2);

close FILE2 or die "Cannot close file2: $!";
close FILE1 or die "Cannot close file1: $!";

sub hello { print $_[0] "Hello\n\n"; }

sub goodbye { print $_[0] "... Goodbye!\n"; }

__END__


Please do read the posting guidelines for this group.

Sinan
 
P

Perl Learner

excellent replies. i like the $outfile ($input_fh) idea. it is
working great.

thanks.
 
A

axel

A. Sinan Unur said:
sub hello { print $_[0] "Hello\n\n"; }
^^^^^
|||||
Interesting, the copy of Perl that I'm using (5.6.0) at the moment
doesn't like the use of an array element in this position. Time
to upgrade this Perl installation I suppose.

Axel
 
A

axel

A. Sinan Unur said:
sub hello { print $_[0] "Hello\n\n"; }
^^^^^
|||||
Interesting, the copy of Perl that I'm using (5.6.0) at the moment
doesn't like the use of an array element in this position. Time
to upgrade this Perl installation I suppose.

Same with Perl 5.8.7.

Axel
 
A

A. Sinan Unur

(e-mail address removed) wrote in @fe2.news.blueyonder.co.uk:
A. Sinan Unur said:
sub hello { print $_[0] "Hello\n\n"; }
^^^^^
|||||
Interesting, the copy of Perl that I'm using (5.6.0) at the moment
doesn't like the use of an array element in this position. Time
to upgrade this Perl installation I suppose.

Same with Perl 5.8.7.

I am sorry, it is my fault, I edited the code a little in the newsreader
after pasting, and before posting. The corrected code is below:

#! /usr/bin/perl

use strict;
use warnings;

open FILE1, '>', 'file1' or die "Cannot open file1: $!";
open FILE2, '>', 'file2' or die "Cannot open file2: $!";

hello ($_) for (*FILE1, *FILE2);
goodbye($_) for (*FILE1, *FILE2);

close FILE2 or die "Cannot close file2: $!";
close FILE1 or die "Cannot close file1: $!";

sub hello { print {$_[0]} "Hello\n\n" }
sub goodbye { print {$_[0]} "... Goodbye!\n" }

__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top