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!

Filehandel Open? 1

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
US
How can you tell if a filehandle is already open?

i.e. open(OFH,">$filename");

if OFH is open do something;
 
use Scalar::Util qw(openhandle);

if (openhandle($LogFile)){--do something with open file--}
else{--do something else: file is not open)}


_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
azzazzello, what am i doing wrong. This doesn't work:

use Scalar::Util qw(openhandle);
my $ofh = 'test.out';
open(OFH,">$ofh");

if ( openhandle($ofh) ) {
print "--do something with open file--\n";
}
else{
print "--do something else: file is not open\n";
}
 
use Scalar::Util qw(openhandle);

my $FH = undef;
my $ofh = 'test.out';
open($FH,">$ofh") or die "can not open file\n";

if (openhandle($FH) ) {
print "--do something with open file--\n";
}
else{
print "--do something else: file is not open\n";
}

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
azzazzello, what am i doing wrong. This doesn't work:

use Scalar::Util qw(openhandle);
my $ofh = 'test.out';
open(OFH,">$ofh");

if ( openhandle($ofh) ) {
print "--do something with open file--\n";
}
else{
print "--do something else: file is not open\n";
}

You're checking the filename ($ofh), not the filehandle (OFH).

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top