Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variables for File Handles

Status
Not open for further replies.

erdman

MIS
Apr 1, 2005
9
US
Is it possible to use a variable for a file handle.

EX.
sub printReport($handle,@array,$arraySize){
for ($i=0;$i<$arraySize;$i++){
print ($handle "$array[$i]\n");
}#end for
}#end sub printReport
 
Here an example. Cheers


$file =$ARGV[0];
$/=undef;
open FH, "<$file";
$perlcode=<FH>;
close FH;
$perlcode=~ s/\r//g;
open FH, ">$file";
print FH "$perlcode";
close FH;



dmazzini
GSM System and Telecomm Consultant

 
Why would you want to use a variable file handle? There should be no need to. This is also not correct syntax:

sub printReport($handle,@array,$arraySize){

that's not the way to import arguments to your sub routine, it should be:

Code:
sub printReport {
   my ($handle,$arraySize,@array) = @_;
   ....
}

note that the array should be the last argument if you don't use references and there is more than one argument.
 
dmazzini, I don't see where you're using a variable as a file handle?

erdman, you can do it like this.
Code:
#!perl
use strict;
use warnings;

my $HANDLE;
open($HANDLE, ">erdman.txt") || die qq(Can't open "erdman.txt"!\n);
my @array = qw(apples oranges peaches pears grapes);
printReport($HANDLE, @array);
close($HANDLE) || die qq(Can't close "erdman.txt"!\n);

[b]sub printReport {
    my ($HANDLE, @array) = @_;
    for (my $i=0;$i<@array;$i++){
        print ($HANDLE "$array[$i]\n");[/b]
    }#end for
}#end sub printReport
Note that the header of your subroutine is not valid Perl. Also the parameter arraySize is not necessary. And if you pass an array or hash to a sub, it must be the last argument. (You can get around this by using references.)

HTH




 
Yes you are right,

I am assigning the content of the filehandle to the variable.... $perlcode=<FH>;

I need to wake up! Thanks
:)

dmazzini
GSM System and Telecomm Consultant

 
Just curious:

What is the purpose of using a variable for a filehandle? Why would it be necessary?
 
I am writing out a couple of files that are utilizing the same output structure. In order to save lines of code I have created a subroutine to perform the action. After I asked the question I went to my books and found what was needed. The files change sizes due to removal of duplicates.

&printDistinct("DISTINCT",$distGroups,0,@scratch);

sub printDistinct(){
($handle,$size,$start,@array)=@_;
for ($i=$start;$i<$size;$i++){
print ($handle "$array[$i]\n");
}#end for
}#end sub printReport
 
In some cases you might want to store filehandles in an array or other type of data structure:
Code:
open (LOG1,">",$logfile1);
open (LOG2,">",$logfile2);

@logfh = (*LOG1, *LOG2);

print $_ "hello" for @logfh;

It's actually a good idea to always use lexical scalars for filehandles - of course you should always close your filehandles, but in the following example it will be closed when it goes out of scope.
Code:
{ 
open (my $fh,">",$file);
print $fh "hello";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top