名前¶
autodie - Replace functions with ones that succeed or die with lexical scope
autodie - レキシカルスコープ内の関数を、成功しなければ die するものに置き換える
概要¶
use autodie; # Recommended: implies 'use autodie qw(:default)'
use autodie qw(:all); # Recommended more: defaults and system/exec.
use autodie qw(open close); # open/close succeed or die
open(my $fh, "<", $filename); # No need to check!
{
no autodie qw(open); # open failures won't die
open(my $fh, "<", $filename); # Could fail silently!
no autodie; # disable all autodies
}
説明¶
bIlujDI' yIchegh()Qo'; yIHegh()!
It is better to die() than to return() in failure.
失敗して return() するより die() した方がましだ。
-- Klingon programming proverb.
-- クリンゴンのプログラミングことわざ。
The autodie pragma provides a convenient way to replace functions that normally return false on failure with equivalents that throw an exception on failure.
autodie プラグマは、通常失敗時に偽を返す関数を、失敗時に例外を投げる 等価な関数に置き換える便利な手段を提供します。
The autodie pragma has lexical scope, meaning that functions and subroutines altered with autodie will only change their behaviour until the end of the enclosing block, file, or eval.
autodie プラグマは レキシカルスコープ を持ちます; つまり、 autodie で置き換えられる関数とサブルーチンは、ブロック、ファイル、 eval の最後までだけ振る舞いを変えます。
If system is specified as an argument to autodie, then it uses IPC::System::Simple to do the heavy lifting. See the description of that module for more information.
autodie の引数に system が指定されると、重い作業をするために IPC::System::Simple を使います。 さらなる情報についてはこのモジュールの説明を参照してください。
例外¶
Exceptions produced by the autodie pragma are members of the autodie::exception class. The preferred way to work with these exceptions under Perl 5.10 is as follows:
autodie プラグマによって生成される例外は、 autodie::exception クラスのメンバーです。 Perl 5.10 でこれらの例外を扱うのに適した方法は次のようなものです:
use feature qw(switch);
eval {
use autodie;
open(my $fh, '<', $some_file);
my @records = <$fh>;
# Do things with @records...
close($fh);
};
given ($@) {
when (undef) { say "No error"; }
when ('open') { say "Error from open"; }
when (':io') { say "Non-open, IO error."; }
when (':all') { say "All other autodie errors." }
default { say "Not an autodie error at all." }
}
Under Perl 5.8, the given/when structure is not available, so the following structure may be used:
Perl 5.8 では、given/when 構文は利用できないので、次のような 構文が使われます:
eval {
use autodie;
open(my $fh, '<', $some_file);
my @records = <$fh>;
# Do things with @records...
close($fh);
};
if ($@ and $@->isa('autodie::exception')) {
if ($@->matches('open')) { print "Error from open\n"; }
if ($@->matches(':io' )) { print "Non-open, IO error."; }
} elsif ($@) {
# A non-autodie exception.
}
See autodie::exception for further information on interrogating exceptions.
例外の問い合わせに関するさらなる情報については autodie::exception を 参照してください。
カテゴリ¶
Autodie uses a simple set of categories to group together similar built-ins. Requesting a category type (starting with a colon) will enable autodie for all built-ins beneath that category. For example, requesting :file will enable autodie for close, fcntl, fileno, open and sysopen.
autodie は、似たような組み込み関数をまとめるために単純なカテゴリの集合を 使います。 (コロンで始まる)カテゴリ型を要求すると、そのカテゴリに属する全ての 組み込み関数に関して autodie を有効にします。 例えば、:file を要求すると、close, fcntl, fileno, open and sysopen の autodie を有効にします。
The categories are currently:
現在のカテゴリは:
:all
:default
:io
read
seek
sysread
sysseek
syswrite
:dbm
dbmclose
dbmopen
:file
binmode
close
fcntl
fileno
flock
ioctl
open
sysopen
truncate
:filesys
chdir
closedir
opendir
link
mkdir
readlink
rename
rmdir
symlink
unlink
:ipc
pipe
:msg
msgctl
msgget
msgrcv
msgsnd
:semaphore
semctl
semget
semop
:shm
shmctl
shmget
shmread
:socket
accept
bind
connect
getsockopt
listen
recv
send
setsockopt
shutdown
socketpair
:threads
fork
:system
system
exec
Note that while the above category system is presently a strict hierarchy, this should not be assumed.
前述のカテゴリシステムは今のところ厳密な階層になっていますが、 これを仮定するべきではないことに注意してください。
A plain use autodie implies use autodie qw(:default). Note that system and exec are not enabled by default. system requires the optional IPC::System::Simple module to be installed, and enabling system or exec will invalidate their exotic forms. See "BUGS" below for more details.
単なる use autodie は use autodie qw(:default) を示します。 system と exec はデフォルトでは有効にならないことに注意してください。 system はオプションの IPC::System::Simple モジュールが インストールされる必要があり、 system と exec を有効にすると、その非標準的な形式は無効になります。 さらなる詳細については後述する "BUGS" を参照してください。
The syntax:
この文法は:
use autodie qw(:1.994);
allows the :default list from a particular version to be used. This provides the convenience of using the default methods, but the surety that no behavorial changes will occur if the autodie module is upgraded.
特定のバージョンの :default リストが使われるようにします。 これは、デフォルト手法を使う便利さを提供しますが、 autodie モジュールがアップグレードされても振る舞いの変更が起こらないことを 保証します。
autodie can be enabled for all of Perl's built-ins, including system and exec with:
autodie は、次のようにすることで system と exec を含む 全ての Perl 組み込み関数に関して有効になります:
use autodie qw(:all);
関数固有の注意¶
flock¶
It is not considered an error for flock to return false if it fails to an EWOULDBLOCK (or equivalent) condition. This means one can still use the common convention of testing the return value of flock when called with the LOCK_NB option:
flock が EWOULDBLOCK (または等価な)条件で失敗したことで 偽を返した場合はエラーとはみなしません。 これは、flock を LOCK_NB オプション付きで呼び出したときの 返り値をテストするためのよくある観衆を使っていることを意味します:
use autodie;
if ( flock($fh, LOCK_EX | LOCK_NB) ) {
# We have a lock
}
Autodying flock will generate an exception if flock returns false with any other error.
flock がその他のエラーで偽を返したときは autodie 下の flock は 例外を生成します。
system/exec¶
The system built-in is considered to have failed in the following circumstances:
system 組み込み関数は、次の条件のときに失敗したとして扱われます:
-
The command does not start.
コマンドが開始しない。
-
The command is killed by a signal.
コマンドがシグナルで kill された。
-
The command returns a non-zero exit value (but see below).
コマンドが非 0 終了コードを返した (しかし後述)。
On success, the autodying form of system returns the exit value rather than the contents of $?.
成功時、autodie 形式の system は $? ではなく 終了コード を 返します。
Additional allowable exit values can be supplied as an optional first argument to autodying system:
autodie 形式の system のオプションの最初の引数として、 追加の受け入れ可能な終了コードを指定できます:
system( [ 0, 1, 2 ], $cmd, @args); # 0,1,2 are good exit values
autodie uses the IPC::System::Simple module to change system. See its documentation for further information.
autodie は system を変更するのに IPC::System::Simple モジュールを 使います。 さらなる情報についてはその文書を参照してください。
Applying autodie to system or exec causes the exotic forms system { $cmd } @args or exec { $cmd } @args to be considered a syntax error until the end of the lexical scope. If you really need to use the exotic form, you can call CORE::system or CORE::exec instead, or use no autodie qw(system exec) before calling the exotic form.
system や exec に autodie を適用すると、レキシカルスコープの 終わりまで、非標準の system { $cmd } @args や exec { $cmd } @args 形式は文法エラーとして扱われます。 もし本当に非標準形式を使う必要がある場合は、 代わりに CORE::system や CORE::exec を呼び出すか、 非標準形式の呼び出し前に no autodie qw(system exec) を使ってください。
コツ¶
Functions called in list context are assumed to have failed if they return an empty list, or a list consisting only of a single undef element.
リストコンテキストで呼び出された関数は、空リストを返したり、 一つの undef 要素だけからなるリストを返した場合、失敗したと仮定されます。
診断¶
- :void cannot be used with lexical scope
-
The
:voidoption is supported in Fatal, but notautodie. To workaround this,autodiemay be explicitly disabled until the end of the current block withno autodie. To disable autodie for only a single function (eg, open) useno autodie qw(open).:voidオプションは Fatal では対応していますが、autodieでは対応していません。 これを回避するために、autodieはno autodieを使って ブロックの終わりまで明示的に無効にできます。 単一の関数(例: open)だけに対して autodie を無効にするには、no autodie qw(open)を使ってください。 - No user hints defined for %s
-
You've insisted on hints for user-subroutines, either by pre-pending a
!to the subroutine name itself, or earlier in the list of arguments toautodie. However the subroutine in question does not have any hints available.サブルーチン名自身の前か、
autodieの引数の一覧に!を付けることで、 ユーザーサブルーチンのヒントを主張しました。 しかし問題のサブルーチンは何のヒントも利用できません。
See also "DIAGNOSTICS" in Fatal.
"DIAGNOSTICS" in Fatal も参照してください。
バグ¶
"Used only once" warnings can be generated when autodie or Fatal is used with package filehandles (eg, FILE). Scalar filehandles are strongly recommended instead.
autodie や Fatal がパッケージファイルハンドル (例: FILE) で 使われると "Used only once" 警告が発生することがあります。 代わりにスカラファイルハンドルを強く勧めます。
When using autodie or Fatal with user subroutines, the declaration of those subroutines must appear before the first use of Fatal or autodie, or have been exported from a module. Attempting to use Fatal or autodie on other user subroutines will result in a compile-time error.
ユーザーサブルーチンに対して autodie や Fatal を使うとき、 これらのサブルーチンの宣言は、 Fatal や autodie が最初に使われるか、モジュールから エクスポートされる前に行われなければなりません。 その他のユーザーラブルーチンに対して Fatal や autodie を使おうとすると、 コンパイル時エラーになります。
Due to a bug in Perl, autodie may "lose" any format which has the same name as an autodying built-in or function.
Perl のバグにより、autodie は autodie する関数と同じ名前のフォーマットを 「失う」ことがあります。
autodie may not work correctly if used inside a file with a name that looks like a string eval, such as eval (3).
autodie は、eval (3) のような、文字列 eval のように見える名前の ファイルの内側で使われると正しく動作しないかもしれません。
autodie と文字列 eval¶
Due to the current implementation of autodie, unexpected results may be seen when used near or with the string version of eval. None of these bugs exist when using block eval.
autodie の現在の実装の都合により、文字列版の eval やその近くでは、 想定外の結果が表れることがあります。 これらのバグはブロック eval を使うと起こりません。
Under Perl 5.8 only, autodie does not propagate into string eval statements, although it can be explicitly enabled inside a string eval.
Perl 5.8 の基だけでは、autodie は文字列 eval 文の中には 伝搬 しません; しかし文字列 eval の中で明示的に有効にすることはできます。
Under Perl 5.10 only, using a string eval when autodie is in effect can cause the autodie behaviour to leak into the surrounding scope. This can be worked around by using a no autodie at the end of the scope to explicitly remove autodie's effects, or by avoiding the use of string eval.
Perl 5.10 の基だけでは、autodie が有効のときに文字列 eval を使うと、 autodie の振る舞いを周りのスコープに漏らすことがあります。 これは、スコープの末尾で no autodie を使うことで 明示的に autodie の効果を取り除くか、 文字列 eval の使用を避けることで回避できます。
None of these bugs exist when using block eval. The use of autodie with block eval is considered good practice.
これらのバグはブロック eval には存在しません。 ブロック eval で autodie を使うのは良い習慣と考えられます。
バグ報告¶
Please report bugs via the CPAN Request Tracker at http://rt.cpan.org/NoAuth/Bugs.html?Dist=autodie.
どうか http://rt.cpan.org/NoAuth/Bugs.html?Dist=autodie の CPAN Request Tracker 経由でバグを報告してください。
フィードバック¶
If you find this module useful, please consider rating it on the CPAN Ratings service at http://cpanratings.perl.org/rate?distribution=autodie .
このモジュールが有用だと感じたら、 http://cpanratings.perl.org/rate?distribution=autodie の CPAN Ratings service で評価することを検討してください。
The module author loves to hear how autodie has made your life better (or worse). Feedback can be sent to <pjf@perltraining.com.au>.
モジュール作者は autodie があなたの生活をどのように良くした (あるいは悪くした)かを聞くのが好きです。 フィードバックは <pjf@perltraining.com.au> に送ることができます。
作者¶
Copyright 2008-2009, Paul Fenwick <pjf@perltraining.com.au>
ライセンス¶
This module is free software. You may distribute it under the same terms as Perl itself.
SEE ALSO¶
Fatal, autodie::exception, autodie::hints, IPC::System::Simple
Perl tips, autodie at http://perltraining.com.au/tips/2008-08-20.html
謝辞¶
Mark Reed and Roland Giersig -- Klingon translators.
See the AUTHORS file for full credits. The latest version of this file can be found at http://github.com/pfenwick/autodie/tree/master/AUTHORS .