How do i change STDOUT back to the screen

L

lozbrown

Ok this is a very novice question but please help,

I have a subroutine that uses STDOUT:

---------------------------------code--------------------------------------------------
sub savefile {
my $content = get($_[0]);
print "saving file $_[0]";
#download the file

die "Couldn't get $_[0]" unless defined $content;


#Open same file for writing, reusing STDOUT
open (STDOUT, ">$_[1]") or die "Can't open $_[1]: $!\n";
binmode(STDOUT);
print "$content";

#Finish up
close STDOUT;
}
------------------------------End
code-----------------------------------------

but how do I send stdout back to the screen so i can use the print
command an it will appear on the screen.

currently after the subroutine has been called i get this error message

print() on closed filehandle STDOUT at C:\Docum...
 
B

boyd

lozbrown said:
Ok this is a very novice question but please help,

I have a subroutine that uses STDOUT:

---------------------------------code-----------------------------------------
---------
sub savefile {
my $content = get($_[0]);
print "saving file $_[0]";
#download the file

die "Couldn't get $_[0]" unless defined $content;


#Open same file for writing, reusing STDOUT
open (STDOUT, ">$_[1]") or die "Can't open $_[1]: $!\n";
binmode(STDOUT);
print "$content";

#Finish up
close STDOUT;
}
------------------------------End
code-----------------------------------------

but how do I send stdout back to the screen so i can use the print
command an it will appear on the screen.

currently after the subroutine has been called i get this error message

print() on closed filehandle STDOUT at C:\Docum...

Most folks use another file handle than STDOUT to do the file writing,
leaving STDOUT to write to the screen. Others might use STDERR to do
screen output, since it is usually attached to the terminal screen.

To print to a file handle, just put the filehandle after the print
statement, as in:

print STDERR "saving file $_[0]\n";

The way to open a filehandle is
open OUT, ">$_[1]";
binmode(OUT);
in your above script, and use
print OUT "$content";
....
close OUT;

Boyd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top