--- Charles Lu <[EMAIL PROTECTED]> wrote:
> 
>    Lets say I want my program to print to STDOUT unless the user
> specifies that the output goes to a file.  Here is my code:
> 
> 
> 
> my $oldhandle=undef;
> if($user_input ne 'STDOUT') {           #user specify output to a
> file
>     open (OUT,">$temp.txt") or die "Can't open the file\n";
>     $oldhandle = select OUT;
> }
> 
> print "blah blah blah\n";
> 
> select ($oldhandle) if(defined($oldhandle));  #restore STDOUT as
> default
> 
> At this point I want to close my FileHandle (OUT) if it was open.  My
> 
> question is HOW do I check to see whether a filehandle(OUT) has been
> opened 
> so I can explicitly close it with
> 
> close(OUT);

use FileHandle to create the open file handle in a common scalar, and
don't put anything in it other wise. Then you can check it like a Bool.

 my ($fh,$oldhandle);
 if ($some_condition) {
   $fh = new FileHandle ">$file" or die "$file: $!";
   $oldhandle = select $fh;
 }
 # . . . .
 close $fh if $fh;




__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to