in reply to Re^2: How can you check to see if a file handle is already open?
in thread How can you check to see if a file handle is already open?

I could have made it clearer ;-)

You can actually avoid opening all the file handles with a small sub:

my %handles; sub handle_for_name { my $f = shift; if ($handles{$f}){ return $handles{$f}; } else { open my $h, '<', $f or die "Can't open file '$f': $!"; $hanldes{$f} = $h; return $h; } }

And then whenever you need a file handle you just call that function that opens the file if it's not already opened.

You can make that sub even smaller with a neat little trick:

use Memoize; memoize("handle_for_name"); sub handle_for_name { my $f = shift; open my $h, '<', $f or die "Can't open file '$f': $!"; return $h; }

That's magic, eh?

To answer your original question: if you have a file handle in a lexical variable $h, you can use if ($fh){ print "file opened\n"}