IO::File::new_tmpfile()

Perl-5.8.8 の source をしばらく見てたんだけど、File::Temp 以外にも一時ファイルを作る module があるようだなあ。

その一つがこれまた Perl-5.8.8 標準添付の IO::File で、

my $fh = IO::File->new_tmpfile();

でファイルハンドルを取得できる。
こいつは中で perl-5.8.8/perlio.c の PerlIO_tmpfile() を使ってるんだけど、こいつがさらに mkstemp() か tmpfile() をコールするようになっているので( compile の仕方とか環境で変わるような)、これを使えば良いんじゃないかな?File::Temp よりも Perl 組み込み度が高そうだし、XS( というか単に C ) だし。

% perldoc perlapio から抜粋

PerlIO_tmpfile()
   This corresponds to tmpfile(), i.e., returns an anonymous PerlIO or
   NULL on error.  The system will attempt to automatically delete the
   file when closed.  On Unix the file is usually "unlink"-ed just
   after it is created so it does not matter how it gets closed. On
   other systems the file may only be deleted if closed via Per-
   lIO_close() and/or the program exits via "exit".  Depending on the
   implementation there may be "race conditions" which allow other
   processes access to the file, though in general it will be safer in
   this regard than ad. hoc. schemes.

module 等を一切使わずに

sysopen my $fh, $path, O_RDWR|O_CREAT|O_EXCL;

でもいいけど、色々面倒見るのが大変だし、コードわかりにくくなるし、既に発明された車輪があるのでそれを使えば良いんじゃないかな?

他にも File::MkTemp(ちと古げ) とか Apache::TempFile(mod_perl 用?) とか、車輪はまだまだあるみたい(詳しく見るのはよしておくよ)。