Home > Archive > PERL Miscellaneous > June 2005 > sending file handle as function argument
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
sending file handle as function argument
|
|
| Perl Learner 2005-06-08, 3:59 pm |
| 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_handl
e
{
my (??????) = @_;
}
i would appreciate it if someone could give me a direction here.
thanks.
| |
| Arne Ruhnau 2005-06-08, 3:59 pm |
| Perl Learner wrote:
> 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_handl
e
> {
> my (??????) = @_;
>
> }
You can use
open my $outfile, ">someoutput.dat";
instead.
perldoc -f open
hth,
Arne Ruhnau
| |
| optional 2005-06-08, 3:59 pm |
| File handles are in global scope. You can open them in one function
and use them in a different function or main.
| |
| A. Sinan Unur 2005-06-08, 3:59 pm |
| "optional" <sivasubramanian.shanmughom@citigroup.com> wrote in
news:1118240985.699641.42850@o13g2000cwo.googlegroups.com:
[ 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
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Perl Learner 2005-06-08, 3:59 pm |
| excellent replies. i like the $outfile ($input_fh) idea. it is
working great.
thanks.
| |
| axel@white-eagle.invalid.uk 2005-06-09, 3:58 pm |
| A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> 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
| |
| axel@white-eagle.invalid.uk 2005-06-09, 8:57 pm |
| axel@white-eagle.invalid.uk wrote:
> A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> ^^^^^
> |||||
> 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. Sinan Unur 2005-06-09, 8:57 pm |
| axel@white-eagle.invalid.uk wrote in news:Z30qe.242558$Cq2.24027
@fe2.news.blueyonder.co.uk:
> axel@white-eagle.invalid.uk wrote:
>
> 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__
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
| |
| Tad McClellan 2005-06-10, 3:59 am |
| Perl Learner <perl707@gmail.com> wrote:
> 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.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
|
|
|
|
|