Hello,
The following (not very Perl-ish) code
#!/usr/bin/perl
if (! -e "mydir/")
{
print "directory doesn't exist.\n";
}
open (my $fh, ">", "mydir/file.txt");
if ($fh)
{
print "file opened.\n";
print $fh;
print $fh "some text\n" or die "failed to write to file.\n";
close ($fh);
}
else
{
print "failed to open file.\n";
}
produces the output such as this
directory doesn't exist.
file opened.
failed to write to file.
GLOB(0x...some-hex-digits...)
Why is $fh not equivalent to false following the open call? As mydir/ does not exist, I'd expect the attempt to open the file to fail.
I get similar results if the directory and file exist, but the file is read-only.
I've tried this with Perl 5.10.1 on Windows 7 x64, and with Perl 5.10.0 on Fedora-11 Linux.
I'm guessing my file handle test is wrong. I've tried Googling this without luck. I expect it's something obvious, but any hints or links would be much appreciated.
Thanks, Rob.