File-Temp-0.22 > File::Temp
File-Temp-0.22
Other versions:
File-Temp-0.12

名前

File::Temp - return name and handle of a temporary file safely

File::Temp - 一時ファイルの名前とハンドルを安全に返す

概要

  use File::Temp qw/ tempfile tempdir /;

  $fh = tempfile();
  ($fh, $filename) = tempfile();

  ($fh, $filename) = tempfile( $template, DIR => $dir);
  ($fh, $filename) = tempfile( $template, SUFFIX => '.dat');
  ($fh, $filename) = tempfile( $template, TMPDIR => 1 );

  binmode( $fh, ":utf8" );

  $dir = tempdir( CLEANUP => 1 );
  ($fh, $filename) = tempfile( DIR => $dir );

Object interface:

オブジェクトインターフェース:

  require File::Temp;
  use File::Temp ();
  use File::Temp qw/ :seekable /;

  $fh = File::Temp->new();
  $fname = $fh->filename;

  $fh = File::Temp->new(TEMPLATE => $template);
  $fname = $fh->filename;

  $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.dat' );
  print $tmp "Some data\n";
  print "Filename is $tmp\n";
  $tmp->seek( 0, SEEK_END );

The following interfaces are provided for compatibility with existing APIs. They should not be used in new code.

以下のインターフェースは既存の API との互換性のために提供されています。 これらは新しいコードでは使うべきではありません。

MkTemp family:

MkTemp 系:

  use File::Temp qw/ :mktemp  /;

  ($fh, $file) = mkstemp( "tmpfileXXXXX" );
  ($fh, $file) = mkstemps( "tmpfileXXXXXX", $suffix);

  $tmpdir = mkdtemp( $template );

  $unopened_file = mktemp( $template );

POSIX functions:

POSIX 関数:

  use File::Temp qw/ :POSIX /;

  $file = tmpnam();
  $fh = tmpfile();

  ($fh, $file) = tmpnam();

Compatibility functions:

互換関数:

  $unopened_file = File::Temp::tempnam( $dir, $pfx );

説明

File::Temp can be used to create and open temporary files in a safe way. There is both a function interface and an object-oriented interface. The File::Temp constructor or the tempfile() function can be used to return the name and the open filehandle of a temporary file. The tempdir() function can be used to create a temporary directory.

File::Temp は、安全な方法で、一時ファイルを作成し、開くのに使えます。 関数インターフェースとオブジェクト指向インターフェースの両方があります。 File::Temp のコンストラクタと tempfile() 関数は、一時ファイルの名前および 開いたファイルハンドルを返すのに使われます。 tempdir() 関数は、一時ディレクトリを作成するのに使われます。

The security aspect of temporary file creation is emphasized such that a filehandle and filename are returned together. This helps guarantee that a race condition can not occur where the temporary file is created by another process between checking for the existence of the file and its opening. Additional security levels are provided to check, for example, that the sticky bit is set on world writable directories. See "safe_level" for more information.

一時ファイルを作成することのセキュリティの側面が、ファイルハンドルと ファイル名が一緒に返されるということで、強調されます。 これが、ファイルが存在するかをチェックし、ファイルを開いている間に 別のプロセスによってテンポラリファイルが作られる、という 競合条件が起こらないことを保証するのを助けています。 追加のセキュリティレベルが提供されています; 例えば、スティッキービットが全体書き込み可能なディレクトリに セットされているかのチェックです。 詳細は "safe_level" を参照してください。

For compatibility with popular C library functions, Perl implementations of the mkstemp() family of functions are provided. These are, mkstemp(), mkstemps(), mkdtemp() and mktemp().

一般的な C ライブラリ関数との互換性のために、 mkstemp() 関数の仲間の Perl 実装が提供されています。 mkstemp(), mkstemps(), mkdtemp(), mktemp() があります。

Additionally, implementations of the standard POSIX tmpnam() and tmpfile() functions are provided if required.

さらに、もし必要なら、標準の POSIX の実装である tmpnam() と tmpfile() が提供されます。

Implementations of mktemp(), tmpnam(), and tempnam() are provided, but should be used with caution since they return only a filename that was valid when function was called, so cannot guarantee that the file will not exist by the time the caller opens the filename.

mktemp(), tmpanam(), tempnam() の実装は提供されていますが、 注意して使われるべきです; なぜなら、関数が呼ばれると、妥当なファイル名だけを返すので、 呼び出し側がファイルを開く時にそのファイルが存在していないことを 保証できないからです。

Filehandles returned by these functions support the seekable methods.

これらの関数で返されるファイルハンドルは seekable メソッドに対応しています。

オブジェクト指向インターフェース

This is the primary interface for interacting with File::Temp. Using the OO interface a temporary file can be created when the object is constructed and the file can be removed when the object is no longer required.

これは File::Temp と相互作用するための主インターフェースです。 OO インターフェースを使うことで、オブジェクトが作成されたときに一時ファイルが 作成され、オブジェクトが不要になったときにファイルが削除されます。

Note that there is no method to obtain the filehandle from the File::Temp object. The object itself acts as a filehandle. Also, the object is configured such that it stringifies to the name of the temporary file, and can be compared to a filename directly. The object isa IO::Handle and isa IO::Seekable so all those methods are available.

File::Temp オブジェクトからファイルハンドルを得るメソッドはないことに 注意してください。 オブジェクト自身がファイルハンドルとして振る舞います。 また、オブジェクトは文字列化されると一時ファイル名になるように 設定されているので、ファイル名と直接比較できます。 オブジェクトは IO::Handle であり IO::Seekable であるので これらの全てのメソッドが利用可能です。

new

Create a temporary file object.

一時ファイルオブジェクトを作成します。

  my $tmp = File::Temp->new();

by default the object is constructed as if tempfile was called without options, but with the additional behaviour that the temporary file is removed by the object destructor if UNLINK is set to true (the default).

デフォルトではオプションなしで tempfile が呼び出されたかのようにして オブジェクトが作成されますが、追加の振る舞いとして、UNLINK が 真 (これがデフォルトです) の場合、オブジェクトのデストラクタによって 一時ファイルは削除されます。

Supported arguments are the same as for tempfile: UNLINK (defaulting to true), DIR, EXLOCK and SUFFIX. Additionally, the filename template is specified using the TEMPLATE option. The OPEN option is not supported (the file is always opened).

対応している引数は tempfile と同じです: UNLINK (デフォルトは真), DIR, EXLOCK, SUFFIX。 さらに、ファイル名テンプレートは TEMPLATE オプションを使って指定します。 OPEN オプションは対応していません (ファイルは常に開かれます)。

 $tmp = File::Temp->new( TEMPLATE => 'tempXXXXX',
                        DIR => 'mydir',
                        SUFFIX => '.dat');

Arguments are case insensitive.

引数は大文字小文字を無視します。

Can call croak() if an error occurs.

エラーが起きると croak() を呼び出すことがあります。

newdir

Create a temporary directory using an object oriented interface.

オブジェクト指向インターフェースを使って一時ディレクトリを作成します。

  $dir = File::Temp->newdir();

By default the directory is deleted when the object goes out of scope.

デフォルトでは、オブジェクトがスコープ外になるとディレクトリは削除されます。

Supports the same options as the tempdir function. Note that directories created with this method default to CLEANUP => 1.

tempdir 関数と同じオプションに対応しています。 このメソッドで作られたディレクトリはデフォルトで CLEANUP => 1 になることに 注意してください。

  $dir = File::Temp->newdir( $template, %options );
filename

Return the name of the temporary file associated with this object (if the object was created using the "new" constructor).

(このオブジェクトが "new" コンストラクタを使って作成されていれば) このオブジェクトに関連付けられた一時ファイル名を返します。

  $filename = $tmp->filename;

This method is called automatically when the object is used as a string.

このメソッドは、このオブジェクトが文字列として使われると自動的に 呼び出されます。

dirname

Return the name of the temporary directory associated with this object (if the object was created using the "newdir" constructor).

(このオブジェクトが "newdir" コンストラクタを使って作成されていれば) このオブジェクトに関連付けられた一時ディレクトリ名を返します。

  $dirname = $tmpdir->dirname;

This method is called automatically when the object is used in string context.

このメソッドは、このオブジェクトが文字列コンテキストで使われると自動的に 呼び出されます。

unlink_on_destroy

Control whether the file is unlinked when the object goes out of scope. The file is removed if this value is true and $KEEP_ALL is not.

オブジェクトがスコープ外になったときにファイルを削除(unlink)するかどうかを 制御します。 この値が真で、$KEEP_ALL が真でなければ、ファイルは削除されます。

 $fh->unlink_on_destroy( 1 );

Default is for the file to be removed.

デフォルトではファイルは削除されます。

DESTROY

When the object goes out of scope, the destructor is called. This destructor will attempt to unlink the file (using unlink1) if the constructor was called with UNLINK set to 1 (the default state if UNLINK is not specified).

オブジェクトがスコープ外になったとき、デストラクタが呼び出されます。 このデストラクタは、UNLINK を 1 (UNLINK が指定されなかった場合のデフォルトは これです)に設定してコンストラクタが呼び出されていると、(unlink1 を使って) ファイルを削除しようとします。

No error is given if the unlink fails.

削除に失敗してもエラーは発生しません。

If the object has been passed to a child process during a fork, the file will be deleted when the object goes out of scope in the parent.

フォークによってこのオブジェクトが子プロセスに渡されると、 親でこのオブジェクトがスコープ外になったときにファイルは削除されます。

For a temporary directory object the directory will be removed unless the CLEANUP argument was used in the constructor (and set to false) or unlink_on_destroy was modified after creation.

一時ディレクトリオブジェクトについては、コンストラクタで CLEANUP 引数が使われ(そして偽に設定され)るか、 作成後に unlink_on_destroy が変更されない限り、 ディレクトリは削除されます。

If the global variable $KEEP_ALL is true, the file or directory will not be removed.

グローバル変数 $KEEP_ALL が真の場合、ファイルやディレクトリは削除されません。

関数

This section describes the recommended interface for generating temporary files and directories.

この節では、一時ファイルと一時ディレクトリを生成するための、 推奨されるインターフェースについて説明します。

tempfile

This is the basic function to generate temporary files. The behaviour of the file can be changed using various options:

これは基本的な関数で、一時ファイルを生成します。 ファイルの振る舞いは様々なオプションで変更できます:

  $fh = tempfile();
  ($fh, $filename) = tempfile();

Create a temporary file in the directory specified for temporary files, as specified by the tmpdir() function in File::Spec.

File::Spec の tmpdir() 関数で指定される 一時ファイル用のディレクトリに、一時ファイルを作成します。

  ($fh, $filename) = tempfile($template);

Create a temporary file in the current directory using the supplied template. Trailing `X' characters are replaced with random letters to generate the filename. At least four `X' characters must be present at the end of the template.

現在のディレクトリに、提供されたテンプレートを使って、 一時ファイルを作成します。 ファイル名を生成するときに、 末尾の文字 `X' はランダムな文字に置き換えられます。 少なくとも四つの `X' 文字がテンプレートの末尾になければなりません。

  ($fh, $filename) = tempfile($template, SUFFIX => $suffix)

Same as previously, except that a suffix is added to the template after the `X' translation. Useful for ensuring that a temporary filename has a particular extension when needed by other applications. But see the WARNING at the end.

`X' の変換の後に接尾辞が加えられることを除いて、前のものと同じです。 他のアプリケーションで必要とされるときに、 テンポラリファイルに特別の拡張子があるようにしたい場合に便利です。 しかし、最後の警告を参照してください。

  ($fh, $filename) = tempfile($template, DIR => $dir);

Translates the template as before except that a directory name is specified.

ディレクトリの名前が指定されているのを除いて、 前述のものと同様にテンプレートを変換します。

  ($fh, $filename) = tempfile($template, TMPDIR => 1);

Equivalent to specifying a DIR of "File::Spec->tmpdir", writing the file into the same temporary directory as would be used if no template was specified at all.

DIR に "File::Spec->tmpdir" を指定したのと等価で、 テンプレートが全く指定されなかった場合に使われる同じ一時ディレクトリに ファイルを書き込みます。

  ($fh, $filename) = tempfile($template, UNLINK => 1);

Return the filename and filehandle as before except that the file is automatically removed when the program exits (dependent on $KEEP_ALL). Default is for the file to be removed if a file handle is requested and to be kept if the filename is requested. In a scalar context (where no filename is returned) the file is always deleted either (depending on the operating system) on exit or when it is closed (unless $KEEP_ALL is true when the temp file is created).

前述のようにファイル名とファイルハンドルを返しますが、($KEEP_ALL に依存して) プログラムが終了したときに自動的に一時ファイルが削除されます。 デフォルトでは、ファイルは、ファイルハンドルが要求されれば削除され、 ファイル名が要求されれば保持されます。 (ファイル名が返されない)スカラコンテキストでは、(オペレーティングシステムに 依存して)終了時か、(一時ファイルが作成されたときに $KEEP_ALL が真でない限り) 閉じられたときに、ファイルはいつも削除されます。

Use the object-oriented interface if fine-grained control of when a file is removed is required.

いつファイルが削除されるかについての細かい制御が必要な場合は、 オブジェクト指向インターフェースを使ってください。

If the template is not specified, a template is always automatically generated. This temporary file is placed in tmpdir() (File::Spec) unless a directory is specified explicitly with the DIR option.

テンプレートが指定されない場合、テンプレートは常に自動的に生成されます。 この一時ファイルは、DIR オプションで明示的にディレクトリが指定されない限り、 tmpdir()(File::Spec) に置かれます。

  $fh = tempfile( DIR => $dir );

If called in scalar context, only the filehandle is returned and the file will automatically be deleted when closed on operating systems that support this (see the description of tmpfile() elsewhere in this document). This is the preferred mode of operation, as if you only have a filehandle, you can never create a race condition by fumbling with the filename. On systems that can not unlink an open file or can not mark a file as temporary when it is opened (for example, Windows NT uses the O_TEMPORARY flag) the file is marked for deletion when the program ends (equivalent to setting UNLINK to 1). The UNLINK flag is ignored if present.

スカラコンテキストで呼ばれると、ファイルハンドルが返され、 閉じられると、オペレーションシステムが対応していれば(この文書の他の場所に ある tmpfile() を参照してください)、ファイルは自動的に削除されます。 ファイルハンドルだけを持つのなら、これは好ましい方法です; ファイルネームを変更することで競合条件を作ることはありません。 開いているファイルを削除できない、あるいはファイルを開いていると 一時的であるとしてファイルにマークをつけることのできないシステムでは (たとえば、Windows NT は、O_TEMPORARY フラグを使います)、 ファイルは、プログラムの終了時に、削除用のマークがつけられます (UNLINK を 1 に設定するのと同じ)。 UNLINK フラグは、あっても無視されます。

  (undef, $filename) = tempfile($template, OPEN => 0);

This will return the filename based on the template but will not open this file. Cannot be used in conjunction with UNLINK set to true. Default is to always open the file to protect from possible race conditions. A warning is issued if warnings are turned on. Consider using the tmpnam() and mktemp() functions described elsewhere in this document if opening the file is not required.

これはテンプレートを元にしたファイル名を返しますが、そのファイルを 開きません。 UNILINK を真に設定するのと同時には使えません。 デフォルトでは常にファイルを開き、競合条件の可能性から保護します。 警告が有効になっている場合は、、警告が出力されます。 ファイルを開くことを要求しないなら、この文書の他の場所で説明されている tmpnam() と、mktemp() 関数を使うことを検討してください。

If the operating system supports it (for example BSD derived systems), the filehandle will be opened with O_EXLOCK (open with exclusive file lock). This can sometimes cause problems if the intention is to pass the filename to another system that expects to take an exclusive lock itself (such as DBD::SQLite) whilst ensuring that the tempfile is not reused. In this situation the "EXLOCK" option can be passed to tempfile. By default EXLOCK will be true (this retains compatibility with earlier releases).

(例えば BSD 派生のシステムのように)オペレーティングシステムが これに対応しているなら、 ファイルハンドルは O_EXLOCK (排他ファイルロックで開く) で開かれます。 これは、目的が、(DBD::SQLite のように) 自分自身で排他ロックを取って 一時ファイルが再利用されないようにすることを想定している別のシステムに パスを渡すことの場合、問題になることがあります。 この場合、"EXLOCK" オプションを tempfile に渡せます。 デフォルトでは EXLOCK は真です (これは以前のリリースとの互換性を維持します)。

  ($fh, $filename) = tempfile($template, EXLOCK => 0);

Options can be combined as required.

オプションは必要に応じて組み合わせることができます。

Will croak() if there is an error.

エラーがあると croak() します。

tempdir

This is the recommended interface for creation of temporary directories. By default the directory will not be removed on exit (that is, it won't be temporary; this behaviour can not be changed because of issues with backwards compatibility). To enable removal either use the CLEANUP option which will trigger removal on program exit, or consider using the "newdir" method in the object interface which will allow the directory to be cleaned up when the object goes out of scope.

これは、一時ディレクトリを作成するための推奨されるインターフェースです。 デフォルトでは、ディレクトリは終了時に削除されません (つまり、これは一時的ではありません; この振る舞いは過去互換性問題により 変更できません)。 削除を有効にするには、 プログラム終了時に削除を行う CLEANUP オプションを使うか、 オブジェクトインターフェースで "newdir" メソッドを使うことを検討してください; これはオブジェクトがスコープ外になったときにディレクトリを 片付けることができます。

The behaviour of the function depends on the arguments:

この関数の振る舞いは引数によります:

  $tempdir = tempdir();

Create a directory in tmpdir() (see File::Spec).

tmpdir() でディレクトリを作成します (File::Spec を参照してください)。

  $tempdir = tempdir( $template );

Create a directory from the supplied template. This template is similar to that described for tempfile(). `X' characters at the end of the template are replaced with random letters to construct the directory name. At least four `X' characters must be in the template.

提供されたテンプレートから、ディレクトリを作成します。 このテンプレートは tempfile() で説明されたものと似ています。 テンプレートの末尾の `X' 文字はランダムな文字に置き換えられ、 ディレクトリ名を作ります。 テンプレートには少なくとも四つ `X' 文字がなければいけません。

  $tempdir = tempdir ( DIR => $dir );

Specifies the directory to use for the temporary directory. The temporary directory name is derived from an internal template.

一時ディレクトリに使うディレクトリを指定します。 一時ディレクトリ名は、内部のテンプレートから生成されます。

  $tempdir = tempdir ( $template, DIR => $dir );

Prepend the supplied directory name to the template. The template should not include parent directory specifications itself. Any parent directory specifications are removed from the template before prepending the supplied directory.

指定されたディレクトリ名をテンプレートに前置します。 テンプレートは、親ディレクトリ指定そのものを含むべきではありません。 指定されたディレクトリを前置する前に、親ディレクトリ指定は テンプレートから削除されます。

  $tempdir = tempdir ( $template, TMPDIR => 1 );

Using the supplied template, create the temporary directory in a standard location for temporary files. Equivalent to doing

指定された template を使うと、一時ファイルのための標準の場所に、 一時ディレクトリを作ります。 次のようにするのと同じですが:

  $tempdir = tempdir ( $template, DIR => File::Spec->tmpdir);

but shorter. Parent directory specifications are stripped from the template itself. The TMPDIR option is ignored if DIR is set explicitly. Additionally, TMPDIR is implied if neither a template nor a directory are supplied.

より短くなります。 親ディレクトリ指定はテンプレート自身から除去されます。 DIR が明示的に設定されていれば TMPDIR オプションは無視されます。 加えて、テンプレートもディレクトリも指定されていなければ、 TMPDIR が仮定されます。

  $tempdir = tempdir( $template, CLEANUP => 1);

Create a temporary directory using the supplied template, but attempt to remove it (and all files inside it) when the program exits. Note that an attempt will be made to remove all files from the directory even if they were not created by this module (otherwise why ask to clean it up?). The directory removal is made with the rmtree() function from the File::Path module. Of course, if the template is not specified, the temporary directory will be created in tmpdir() and will also be removed at program exit.

一時ディレクトリを提供されたテンプレートを使って作成しますが、 プログラムが終了するときに、そのディレクトリ(およびその中の全てのファイル)を 削除しようとします。 この試みは、このモジュールが作っていないものも含めて、 そのディレクトリから全てのファイルを削除しようとします (さもなければなぜ削除するか尋ねますか?)。 ディレクトリの除去は File::Path の rmtree() 関数で 行なわれます。 もちろん、テンプレートが指定されていなければ、 一時ディレクトリは tempdir() に作られ、プログラムの終了時に削除されます。

Will croak() if there is an error.

エラーがあると croak() します。

MKTEMP 関数

The following functions are Perl implementations of the mktemp() family of temp file generation system calls.

以下の関数は、一時ファイルを生成するシステムコールである mktemp() の仲間の Perl の実装です。

mkstemp

Given a template, returns a filehandle to the temporary file and the name of the file.

テンプレートを与えると、一時ファイルのファイルハンドルとファイル名を返します。

  ($fh, $name) = mkstemp( $template );

In scalar context, just the filehandle is returned.

スカラコンテキストでは、ファイルハンドルだけが返されます。

The template may be any filename with some number of X's appended to it, for example /tmp/temp.XXXX. The trailing X's are replaced with unique alphanumeric combinations.

テンプレートは、いくつかの `X' が追加されたファイル名かもしれません; 例えば /tmp/tmp.XXXX。 末尾の X は、ユニークな英数字の組合せに置き換えられます。

Will croak() if there is an error.

エラーがあると croak() します。

mkstemps

Similar to mkstemp(), except that an extra argument can be supplied with a suffix to be appended to the template.

mkstemp() に似ていますが、特別な引数を与えて、 テンプレートに接尾辞を追加できます。

  ($fh, $name) = mkstemps( $template, $suffix );

For example a template of testXXXXXX and suffix of .dat would generate a file similar to testhGji_w.dat.

例えば、テンプレート が testXXXXXX で接尾辞が .dat なら、 testthGji_w.dat のようなファイルを生成します。

Returns just the filehandle alone when called in scalar context.

スカラコンテキストで呼ばれると、ただファイル名だけを返します。

Will croak() if there is an error.

エラーがあると croak() します。

mkdtemp

Create a directory from a template. The template must end in X's that are replaced by the routine.

テンプレートからディレクトリを作成します。 ルーチンによって置き換えられるテンプレートは、 複数の X で終っていなければなりません。

  $tmpdir_name = mkdtemp($template);

Returns the name of the temporary directory created.

作成された一時ディレクトリの名前を返します。

Directory must be removed by the caller.

ディレクトリは、呼び出し元で削除しなければなりません。

Will croak() if there is an error.

エラーがあると croak() します。

mktemp

Returns a valid temporary filename but does not guarantee that the file will not be opened by someone else.

妥当な一時ファイル名を返しますが、そのファイルが他の誰かに 開かれていないことは保証しません。

  $unopened_file = mktemp($template);

Template is the same as that required by mkstemp().

テンプレートは、mkstemp() で必要とされるのと同じです。

Will croak() if there is an error.

エラーがあると croak() します。

POSIX 関数

This section describes the re-implementation of the tmpnam() and tmpfile() functions described in POSIX using the mkstemp() from this module.

この節は、このモジュールから mkstemp() を使っている、POSIX に 説明されている tmpnam() と tmpfile() 関数の再実装を説明しています。

Unlike the POSIX implementations, the directory used for the temporary file is not specified in a system include file (P_tmpdir) but simply depends on the choice of tmpdir() returned by File::Spec. On some implementations this location can be set using the TMPDIR environment variable, which may not be secure. If this is a problem, simply use mkstemp() and specify a template.

POSIX の実装とは違い、一時ファイルに使われるディレクトリは、 システム内部のファイル (P_tmpdir) で指定されているのではなく、 単純に、File::Spec によって、tmpdir() が返すものの選択に 依っています。 実装によっては、この場所は、安全ではないかもしれない TMPDIR 環境変数を 使って設定されているかもしれません。 このことが問題なら、単純に、mkstemp() を使い、テンプレートを指定して下さい。

tmpnam

When called in scalar context, returns the full name (including path) of a temporary file (uses mktemp()). The only check is that the file does not already exist, but there is no guarantee that that condition will continue to apply.

スカラコンテキストで呼ばれたら、(mktemp() を使う)一時ファイルの (パスを含んだ)フルネームを返します。 ファイルが既に存在しないことだけをチェックしますが、 その状態が適用され続ける保証はありません。

  $file = tmpnam();

When called in list context, a filehandle to the open file and a filename are returned. This is achieved by calling mkstemp() after constructing a suitable template.

リストコンテキストで呼ばれると、ファイルを開くファイルハンドルと ファイル名が返ります。 これは、適切なテンプレートを組み立てた後に mkstemp() を呼ぶことで 達成されます。

  ($fh, $file) = tmpnam();

If possible, this form should be used to prevent possible race conditions.

可能なら、潜在的な競合状態を防ぐためにこの形で使うべきです。

See "tmpdir" in File::Spec for information on the choice of temporary directory for a particular operating system.

特定のオペレーティングシステムで 一時ディレクトリを選択するための情報については、 "tmpdir" in File::Spec を参照してください。

Will croak() if there is an error.

エラーがあると croak() します。

tmpfile

Returns the filehandle of a temporary file.

一時ファイルのファイルハンドルを返します。

  $fh = tmpfile();

The file is removed when the filehandle is closed or when the program exits. No access to the filename is provided.

ファイルハンドルが閉じられるときか、プログラムが終了するときに、 ファイルは削除されます。 ファイル名へのアクセスは提供されていません。

If the temporary file can not be created undef is returned. Currently this command will probably not work when the temporary directory is on an NFS file system.

一時ファイルが作られなければ、undef が返ります。 一時ディレクトリが NFS ファイルシステムの場合、 現在のところこのコマンドはおそらく動きません。

Will croak() if there is an error.

エラーがあると croak() します。

追加の関数

These functions are provided for backwards compatibility with common tempfile generation C library functions.

これらの関数は、共通の一時ファイル生成の C ライブラリ関数の 後方互換のために提供されています。

They are not exported and must be addressed using the full package name.

エクスポートされていないので、完全なパッケージ名を使って 記述しなければなりません。

tempnam

Return the name of a temporary file in the specified directory using a prefix. The file is guaranteed not to exist at the time the function was called, but such guarantees are good for one clock tick only. Always use the proper form of sysopen with O_CREAT | O_EXCL if you must open such a filename.

接頭辞を使って指定したディレクトリ内に、一時ファイルのファイル名を返します。 ファイルは関数が呼ばれたそのときに存在していないことを保証されます。 ですが、この保証は一瞬だけのものです。 そのようなファイル名を開かなければならないなら、 O_CREAT | O_EXCL で、sysopen の適切な形を常に使ってください。

  $filename = File::Temp::tempnam( $dir, $prefix );

Equivalent to running mktemp() with $dir/$prefixXXXXXXXX (using unix file convention as an example)

$dir/$prefixXXXXXXXXX で、mktemp() を実行するのと同じです。 (例として、unix のファイルの慣例を使います)

Because this function uses mktemp(), it can suffer from race conditions.

この関数は mktemp() を使うので、競合状態の影響を受けます。

Will croak() if there is an error.

エラーがあると croak() します。

ユーティリティ関数

Useful functions for dealing with the filehandle and filename.

ファイルハンドルとファイル名を扱うための便利関数。

unlink0

Given an open filehandle and the associated filename, make a safe unlink. This is achieved by first checking that the filename and filehandle initially point to the same file and that the number of links to the file is 1 (all fields returned by stat() are compared). Then the filename is unlinked and the filehandle checked once again to verify that the number of links on that file is now 0. This is the closest you can come to making sure that the filename unlinked was the same as the file whose descriptor you hold.

開いているファイルハンドルと、関連付けられたファイル名を与えると、 安全に削除します。 これは、ファイル名とファイルハンドルが同じファイルを指し、 ファイルへのリンクの数が、一つであること (stat()が返す全てのフィールドが比較されます)を 最初にチェックすることで、実現します。 それからファイル名が削除されて、ファイルハンドルが再びチェックされ、 そのファイルのリンクの数が今度は 0 であることを検証します。 このことは、保持しているファイルのディスクリプタと削除されたファイル名が 同じであることを確かめるのに、最も周到です。

  unlink0($fh, $path)
     or die "Error unlinking file $path safely";

Returns false on error but croaks() if there is a security anomaly. The filehandle is not closed since on some occasions this is not required.

エラーのときは偽を返しますが、セキュリティ問題があった場合は croak() します。 これが必要でない場合があるので、ファイルハンドルは閉じません。

On some platforms, for example Windows NT, it is not possible to unlink an open file (the file must be closed first). On those platforms, the actual unlinking is deferred until the program ends and good status is returned. A check is still performed to make sure that the filehandle and filename are pointing to the same thing (but not at the time the end block is executed since the deferred removal may not have access to the filehandle).

プラットフォームによっては(例えば、Windows NT)、開いているファイルを 削除できないものもあります (ファイルはまず閉じられなければいけません)。 それらのプラットフォームでは、プログラムが終了し、良い状態になるまで、 実際の削除は据え置かれます。 ファイルハンドルとファイル名が同じのを指す事を確かめるまで、チェックは 行なわれます。 (しかし、最後のブロックが実行された時点でなければ、 据え置かれた削除はファイルハンドルにアクセスしないでしょう。)

Additionally, on Windows NT not all the fields returned by stat() can be compared. For example, the dev and rdev fields seem to be different. Also, it seems that the size of the file returned by stat() does not always agree, with stat(FH) being more accurate than stat(filename), presumably because of caching issues even when using autoflush (this is usually overcome by waiting a while after writing to the tempfile before attempting to unlink0 it).

さらに、Windows NT は、stat() が比較される全てのフィールドを返しません。 例えば、devrdev フィールドは異なっているようです。 また、stat() が返すファイルのサイズは、常には一致しません; stat(FH) は、stat(filename) よりも、狂いがありません; おそらく、例え自動フラッシュを使っていても、キャッシングの問題によるものです (これは普通、一時ファイルに書き込んだ後、それを unlink0 が試される前に しばらく待つことで、対処できます)。

Finally, on NFS file systems the link count of the file handle does not always go to zero immediately after unlinking. Currently, this command is expected to fail on NFS disks.

最後に、NFS ファイルシステムでは、ファイルハンドルのリンクカウントは、 常に削除の直後に 0 になるとは限りません。 現在のところ、このコマンドは、NFS ディスク上では失敗すると想定されます。

This function is disabled if the global variable $KEEP_ALL is true and an unlink on open file is supported. If the unlink is to be deferred to the END block, the file is still registered for removal.

グローバル変数 $KEEP_ALL が真で、開いているファイルの 削除に対応している場合、この関数は無効になります。 削除が END ブロックまで遅延される場合、このファイルは 削除のために登録されたままです。

This function should not be called if you are using the object oriented interface since the it will interfere with the object destructor deleting the file.

この関数はファイルを削除するオブジェクトデストラクタを妨害するので、 オブジェクト指向インターフェースを使っている場合はこの関数は 呼び出されるべきではありません。

cmpstat

Compare stat of filehandle with stat of provided filename. This can be used to check that the filename and filehandle initially point to the same file and that the number of links to the file is 1 (all fields returned by stat() are compared).

ファイルハンドルの stat を、指定されたファイル名の stat と比較します。 これは、 まずファイル名とファイルハンドルが同じファイルを指していて、 そのファイルへのリンクの数が 1 であることをチェックするのに使えます。 (stat() によって返された全てのフィールドが比較されます)。

  cmpstat($fh, $path)
     or die "Error comparing handle with file";

Returns false if the stat information differs or if the link count is greater than 1. Calls croak if there is a security anomaly.

stat 情報が異なるか、リンクカウントが 1 より大きい場合は、偽を返します。 セキュリティ上の異常があると croak を呼び出します。

On certain platforms, for example Windows, not all the fields returned by stat() can be compared. For example, the dev and rdev fields seem to be different in Windows. Also, it seems that the size of the file returned by stat() does not always agree, with stat(FH) being more accurate than stat(filename), presumably because of caching issues even when using autoflush (this is usually overcome by waiting a while after writing to the tempfile before attempting to unlink0 it).

例えば Windows のような、一部のプラットフォームでは、 stat() によって返された全てのフィールドを比較することはできません。 例えば、Windows では devrdev フィールドは異なっているように 見えます。 また、stat() によって返されるファイルサイズは常には一致しません; stat(FH)stat(filename) より正確になります; おそらく、たとえ autoflush を使ってもキャッシュ問題のためです (これは普通、一時ファイルを書いた後、unlink0 を試みる前に、 しばらく待つことで克服されます)。

Not exported by default.

デフォルトではエクスポートされません。

unlink1

Similar to unlink0 except after file comparison using cmpstat, the filehandle is closed prior to attempting to unlink the file. This allows the file to be removed without using an END block, but does mean that the post-unlink comparison of the filehandle state provided by unlink0 is not available.

unlink0 と同様ですが、cmpstat を使ってファイルを比較した後、 ファイルを削除しようとする前にファイルを閉じます。 これにより、END ブロックを使わずにファイルを削除できるようになりますが、 これは、unlink0 によって提供される、ファイルハンドル状態の unlink 後の比較は利用できなくなるということです。

  unlink1($fh, $path)
     or die "Error closing and unlinking file";

Usually called from the object destructor when using the OO interface.

通常は、OO インターフェースを使うときにオブジェクトのデストラクタから 呼び出されます。

Not exported by default.

デフォルトではエクスポートされません。

This function is disabled if the global variable $KEEP_ALL is true.

この関数は、$KEEP_ALL が真の場合は無効になります。

Can call croak() if there is a security anomaly during the stat() comparison.

stat() 比較のときにセキュリティの問題がある場合は croak() を 呼ぶことがあります。

cleanup

Calling this function will cause any temp files or temp directories that are registered for removal to be removed. This happens automatically when the process exits but can be triggered manually if the caller is sure that none of the temp files are required. This method can be registered as an Apache callback.

この関数を呼び出すと、削除すると登録された全ての一時ファイルや 一時ディレクトリが削除されます。 これはプロセスの終了時に自動的に行われますが、 呼び出し元が一時ファイルを必要としていないことを確実にしたい場合は 手動で引き起こすこともできます。 このメソッドは Apache のコールバックとして登録できます。

On OSes where temp files are automatically removed when the temp file is closed, calling this function will have no effect other than to remove temporary directories (which may include temporary files).

一時ファイルが閉じられたときに一時ファイルが自動的に削除される OS では、 この関数を呼び出しても、 (一時ファイルが含まれているかもしれない)一時ディレクトリを削除する以外に 何の効果もありません。

  File::Temp::cleanup();

Not exported by default.

デフォルトではエクスポートされません。

パッケージ変数

These functions control the global state of the package.

これらの関数は、パッケージのグローバルな状態を制御します。

safe_level

Controls the lengths to which the module will go to check the safety of the temporary file or directory before proceeding. Options are:

このモジュールが処理前に一時ファイルか一時ディレクトリを 安全にチェックしようとする程度を制御します。 オプション:

STANDARD

Do the basic security measures to ensure the directory exists and is writable, that temporary files are opened only if they do not already exist, and that possible race conditions are avoided. Finally the unlink0 function is used to remove files safely.

基本的なセキュリティの程度で、以下のことを保証します; ディレクトリが存在し、書き込み可能であること; 一時ファイルは、まだ存在していない場合にのみ開いていて、競合条件が 避けられていること。 最後に、ファイルを安全に削除するために unlink0 関数が使われます。

MEDIUM

In addition to the STANDARD security, the output directory is checked to make sure that it is owned either by root or the user running the program. If the directory is writable by group or by other, it is then checked to make sure that the sticky bit is set.

STANDARD の安全性に加えて、出力ディレクトリが root または プログラムを実行しているユーザのどちらかによって 所有されていることを確かめるチェックをします。 ディレクトリが他のグループかユーザによって書き込み可能であるならば、 スティッキービットがセットされていることを確かめるチェックをします。

Will not work on platforms that do not support the -k test for sticky bit.

スティッキービットのための -k テストに対応していないプラットフォームでは 動作しません。

HIGH

In addition to the MEDIUM security checks, also check for the possibility of ``chown() giveaway'' using the POSIX sysconf() function. If this is a possibility, each directory in the path is checked in turn for safeness, recursively walking back to the root directory.

MEDIUM の安全性チェックに加えて、POSIX sysconf() 関数を使って、 ``chown() giveaway'' の可能性もチェックします。 もしこの可能性があれば、それぞれのパスのディレクトリは、 ルートディレクトリまで再帰的に安全性がチェックされます。

For platforms that do not support the POSIX _PC_CHOWN_RESTRICTED symbol (for example, Windows NT) it is assumed that ``chown() giveaway'' is possible and the recursive test is performed.

POSIX_PC_CHOWN_RESTRICTED シンボルに対応しない プラットフォーム(たとえば、Windows NT)では、 `chown() giveaway'' が可能であると仮定し、再帰的なテストが行われます。

The level can be changed as follows:

レベルは以下のようにして変更できます:

  File::Temp->safe_level( File::Temp::HIGH );

The level constants are not exported by the module.

レベル定数はモジュールによってエクスポートされません。

Currently, you must be running at least perl v5.6.0 in order to run with MEDIUM or HIGH security. This is simply because the safety tests use functions from Fcntl that are not available in older versions of perl. The problem is that the version number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though they are different versions.

MEDIUM か HIGH のセキュリティを実行するためには、 今のところ少なくとも Perl v5.6.0 で動かさなければなりません。 これは単純に、安全なテストは v5.6.0 より古いバージョンの perl では 利用できない <Fcntl|Fcntl> から関数を使うからです。 perl 5.6.0 と、5.005_03 の Fcntl のバージョン番号が、それらは 違うものであるにもかかわらず、 バージョン番号が同じであることが問題です。

On systems that do not support the HIGH or MEDIUM safety levels (for example Win NT or OS/2) any attempt to change the level will be ignored. The decision to ignore rather than raise an exception allows portable programs to be written with high security in mind for the systems that can support this without those programs failing on systems where the extra tests are irrelevant.

HIGH と、MEDIUM の安全レベルをサポートしない OS(Win NT や OS/2) では、 レベルを変更しようとしても無視されます。 例外を起こさずに無視すると言う決定により、 移植性のあるプログラムを、 追加のテストが無意味であるシステムでプログラムが失敗するということなしに、 高い安全性に対応することができるシステムのことを考えて、 高いセキュリティで書くことを可能にします。

If you really need to see whether the change has been accepted simply examine the return value of safe_level.

もし、実際に変更が受け入れられたかどうかを確認する必要があるなら、 単に safe_level の返り値をを調べてください。

  $newlevel = File::Temp->safe_level( File::Temp::HIGH );
  die "Could not change to high security"
      if $newlevel != File::Temp::HIGH;
TopSystemUID

This is the highest UID on the current system that refers to a root UID. This is used to make sure that the temporary directory is owned by a system UID (root, bin, sys etc) rather than simply by root.

これは root UID を参照する現在のシステムでもっとも高い UID です。 これは一時ディレクトリが単純に root のものであるのではなくて、 システム UID のもの(root, bin, sys など)であることを確かめるのに、 使われます。

This is required since on many unix systems /tmp is not owned by root.

多くの unix のシステムで、/tmp は root のものではないので、 これが必要とされます。

Default is to assume that any UID less than or equal to 10 is a root UID.

デフォルトでは、10 以下のものはどんな UID も root UID と想定します。

  File::Temp->top_system_uid(10);
  my $topid = File::Temp->top_system_uid;

This value can be adjusted to reduce security checking if required. The value is only relevant when safe_level is set to MEDIUM or higher.

必要ならばこの値を調整してセキュリティチェックを下げることもできます。 この値は safe_level がMEDIUM 以上に設定されている場合に関連があります。

$KEEP_ALL

Controls whether temporary files and directories should be retained regardless of any instructions in the program to remove them automatically. This is useful for debugging but should not be used in production code.

プログラムが一時ファイルやディレクトリを自動的に削除しようとする 操作をしても、それらを残したままにするかどうかを制御します。 これはデバッグには有用ですが、本番コードで用いるべきではありません。

  $File::Temp::KEEP_ALL = 1;

Default is for files to be removed as requested by the caller.

デフォルトは、呼び出し元によって要求された場合にファイルを削除します。

In some cases, files will only be retained if this variable is true when the file is created. This means that you can not create a temporary file, set this variable and expect the temp file to still be around when the program exits.

場合によっては、ファイルが作られた時点でこの変数が真の場合にのみ ファイルが残されます。 つまり、一時ファイルを作って、この変数を設定しても、 プログラムが終了したときにこの一時ファイルがまだ残っていることを 想定できないということです。

$DEBUG

Controls whether debugging messages should be enabled.

デバッグメッセージを有効にするかどうかを制御します。

  $File::Temp::DEBUG = 1;

Default is for debugging mode to be disabled.

デフォルトは、デバッグモードを無効にします。

警告

For maximum security, endeavour always to avoid ever looking at, touching, or even imputing the existence of the filename. You do not know that that filename is connected to the same file as the handle you have, and attempts to check this can only trigger more race conditions. It's far more secure to use the filehandle alone and dispense with the filename altogether.

最高のセキュリティのためには、あるファイルの存在を、調べたり、 作成したり、仮定したりすることを避けるように努力して下さい。 そのファイル名があなたの持っているハンドルと同じファイルに 接続されているかどうかはわかりませんし、 それを調べようとすることはより多くの競合状態を引き起こすだけです。 ファイルハンドルだけを使い、ファイル名と切り離す方があるかに安全です。

If you need to pass the handle to something that expects a filename then, on a unix system, use "/dev/fd/" . fileno($fh) for arbitrary programs, or more generally "+<=&" . fileno($fh) for Perl programs. You will have to clear the close-on-exec bit on that file descriptor before passing it to another process.

ファイルネームを期待するものにハンドルを渡す必要があるなら、 unix システムでは、任意のプログラムに対して "/dev/fd/" . fileno($fh) を 使ってください; または、より一般的に Perl のプログラムのために "+<=&" . fileno($fh) を使って下さい。 他のプロセスにそれを渡す前にファイル記述子で close-on-exec ビット をクリアする必要があるでそしょう。

    use Fcntl qw/F_SETFD F_GETFD/;
    fcntl($tmpfh, F_SETFD, 0)
        or die "Can't clear close-on-exec flag on temp fh: $!\n";

一時ファイルと NFS

Some problems are associated with using temporary files that reside on NFS file systems and it is recommended that a local filesystem is used whenever possible. Some of the security tests will most probably fail when the temp file is not local. Additionally, be aware that the performance of I/O operations over NFS will not be as good as for a local disk.

NFS ファイルシステムにある一時ファイルを使うことに関してはいくつかの 問題があり、出来る限りローカルのファイルシステムを使うことが 推奨されます。 いくつかのセキュリティテストは、一時ファイルがローカルにない場合、 ほとんどたいてい失敗するでしょう。 更に、NFS 越しの I/O 操作はローカルディスクほど良くないことに 気をつけて下さい。

fork する

In some cases files created by File::Temp are removed from within an END block. Since END blocks are triggered when a child process exits (unless POSIX::_exit() is used by the child) File::Temp takes care to only remove those temp files created by a particular process ID. This means that a child will not attempt to remove temp files created by the parent process.

場合によっては File::Temp によって作られたファイルは END ブロックの中で 削除されます。 (POSIX::_exit() が子で使われていない限り) 子プロセスの終了時に END ブロックが実行されるので、 File::Temp は特定のプロセス ID で作成された一時ファイルのみを削除するように 注意を払います。 つまり、子プロセスは親プロセスによって作られた一時ファイルを 削除しようとしません。

If you are forking many processes in parallel that are all creating temporary files, you may need to reset the random number seed using srand(EXPR) in each child else all the children will attempt to walk through the same set of random file names and may well cause themselves to give up if they exceed the number of retry attempts.

もしそれぞれが一時ファイルを作る多くのプロセスを平行に fork するなら、 それぞれの子で srand(EXPR) を使うことで乱数の種をリセットする必要が あるかもしれません; さもなければ全ての子が同じランダムファイル名の 集合を通過しようとして、再試行回数を超えると諦めてしまうかもしれません。

ディレクトリの削除

Note that if you have chdir'ed into the temporary directory and it is subsequently cleaned up (either in the END block or as part of object destruction), then you will get a warning from File::Path::rmtree().

一時ディレクトリに chdir して、その後(END ブロックやオブジェクト破壊の 一部として)それを片付けると、 File::Path::rmtree() から警告が出ることに注意してください。

BINMODE

The file returned by File::Temp will have been opened in binary mode if such a mode is available. If that is not correct, use the binmode() function to change the mode of the filehandle.

File::Temp から返されたファイルは、(モードがある場合は)バイナリモードで 開かれます。 これが正しくない場合、ファイルハンドルのモードを変更するために binmode() 関数を使ってください。

Note that you can modify the encoding of a file opened by File::Temp also by using binmode().

binmode() を使うことで、File::Temp によって開かれたファイルの エンコーディングを変更できることに注意してください。

歴史

Originally began life in May 1999 as an XS interface to the system mkstemp() function. In March 2000, the OpenBSD mkstemp() code was translated to Perl for total control of the code's security checking, to ensure the presence of the function regardless of operating system and to help with portability. The module was shipped as a standard part of perl from v5.6.1.

もともとは、1999 年の 5 月にシステムの mkstemp() 関数への XS インターフェースとして生まれました。 2000 年 5 月に、コードのセキュリティチェックを完全に制御するために、 OpenBSD の mkstemp() コードが Perl に移植されました; オペレーティングシステムに依存しない関数の存在を保証することになり、 移植性を助けることになりました。 このモジュールは v5.6.1 から perl の標準として配布されています。

SEE ALSO

"tmpnam" in POSIX, "tmpfile" in POSIX, File::Spec, File::Path

See IO::File and File::MkTemp, Apache::TempFile for different implementations of temporary file handling.

一時ファイルを扱う異なった実装については IO::File, File::MkTemp, Apache::TempFile を参照してください。

See File::Tempdir for an alternative object-oriented wrapper for the tempdir function.

もう一つの tempdir 関数のオブジェクト指向のラッパーについては File::Tempdir を参照してください。

作者

Tim Jenness <tjenness@cpan.org>

Copyright (C) 2007-2009 Tim Jenness. Copyright (C) 1999-2007 Tim Jenness and the UK Particle Physics and Astronomy Research Council. All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Original Perl implementation loosely based on the OpenBSD C code for mkstemp(). Thanks to Tom Christiansen for suggesting that this module should be written and providing ideas for code improvements and security enhancements.