Sunday, September 24, 2006

Perl: File handle as function argument

Recently I was writing one function in Perl. And I come across one little problem - I did not know how to pass and file handler to a function. Nothing big, but it gave me a little of googling to find the answer for this.
So, let me just give a solution to this problem, without any further explenation.

use strict;

open(FH,"> some_file.txt");
print FH "Write first line to the file";


sub writeToFile {
my $FH = shift;
print $FH "Write second line to the file";

Hence, one must remember that when we past a file handler to a function in Perl, we must do it using '*', no other way that I know of works.

No comments:

Post a Comment