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

Filehandles and STDOUT

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
US
I'm generating web pages. The script opens a file and writes the web page to it. Page is a class which contains the file open method. This works fine.


my $outfile="../index.html";
my $page=new Page;
my $fh=$page->open($outfile);
if ($fh =~ /ERROR:/) {
die "\n$fh\n";
}


Now I want to use the Page class from a CGI script, which means the $outfile should be STDOUT and work just fine. However I can't get the $fh filehandle to point to SDTOUT.
What am I doing wrong? This doesn't work.


my $outfile="STDOUT";
my $page=new Page;
my $fh=$page->open($outfile);
print "fh=$fh\n";
if ($fh =~ /ERROR:/) {
die "\n$fh\n";
}


This is the open method in the Page class:


sub open {
use FileHandle;
my ($self,$outFile)=@_;
# Open output file and complain on failure
$fh = new FileHandle ">$outFile";
if (!defined $fh) {
return ("ERROR: Could not open $outFile");
}
return $fh;
}


 
You're opening a filehandle for writing, so you can't get the output from the file's contents (i.e. you can't read and write at the same time), you'll need to open two different filehandles if you want to do both things.
 
Thanks for your response.

I solved the problem using the following code in the Page class:


if ($outFile eq "STDOUT") {
$fh = select(STDOUT);
}



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top