use POSIX qw (tmpnam);
use Fcntl;
use IO::File;
do { $f = tmpnam() } until $fh = IO::File->new($f, O_RDWR | O_CREAT | O_EXCL );
print $f;
print $f "$_\n" while $_++ < 10;
seek($f,0,0);
print <$f>;
print "No\n" if (!(-e "foobar.txt"));
print "Yes\n" if -e $f;
END { unlink $f; print "No\n" if (!(-e $f)); }
Line 5 is supposed to print out the temporary file name, it does, somewhat cryptic names like \s2mg, \s2q0 on Windows. The next line is supposed to print the numbers 0 to 9 in that file. Maybe it does, maybe it doesn't, for when I try to print out its contents after two lines, nothing gets printed out. The next line is just a dummy statement, checking whether all is well. The next line checks the existence of the temporary file, it prints out Yes, confirming that the file has indeed been created. However in the END block, even after I have unlinked the temporary file, no No is printed out. Can somebody explain why the file contents and No aren't printed out? Plus another question, I can read from and write to only text files, right? So if the filename is like \s2mg, it means it isn't a text file, how come can I write something to it?

New Topic/Question
Reply



MultiQuote





|