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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Filehandles

Status
Not open for further replies.

hca

Programmer
Sep 25, 2000
5
US
What I am trying to do is write to a single file with multiple filehandles. I want to write different detail lines depending on the result of an if statement. Any sugestions?
 
You may want to rethink the algorithm. Why do you need 5 or 10 open filehandles when one will do? Open files take more system resources, and thus makes your program slower.


Instead of writing to different filehandles, could you write to different files or different positions in the file based on your if statement?
 
You can print to any open HANDLE, or use 'select' to point STDOUT
at the HANDLE you wish to print to.

Code:
#!/usr/local/bin/perl -w
open(HANDLE1,">first_File") or die "failed to open first_File, $!\n";
open(HANDLE2,">second_File") or die "failed to open second_File, $!\n";

# simply print to the desired handle
print HANDLE1 "This should go into first_File\n";
print HANDLE2 "This should go into second_File\n";

# or, use 'select HANDLE' to switch between handles, 
select HANDLE1;
print "This should go into first_File\n";
select HANDLE2;
print "This should go into second_File\n";

close HANDLE1;
close HANDLE2;

HTH Please use descriptive titles and check the FAQs.
And, beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top