argra****@users*****
argra****@users*****
2008年 7月 21日 (月) 05:30:20 JST
Index: docs/perl/5.10.0/perlfaq7.pod diff -u docs/perl/5.10.0/perlfaq7.pod:1.5 docs/perl/5.10.0/perlfaq7.pod:1.6 --- docs/perl/5.10.0/perlfaq7.pod:1.5 Fri Jun 13 03:34:57 2008 +++ docs/perl/5.10.0/perlfaq7.pod Mon Jul 21 05:30:20 2008 @@ -780,14 +780,11 @@ =end original -もう一つのクロージャの使用法は -to make a variable I<private> to a -named subroutine, e.g. a counter that gets initialized at creation -time of the sub and can only be modified from within the sub. -This is sometimes used with a BEGIN block in package files to make -sure a variable doesn't get meddled with during the lifetime of the -package: -(TBT) +もう一つのクロージャの使用法は、ある変数を名前つきサブルーチンで +I<プライベート> にすることです; 例えば、サブルーチンの作成時に初期化され、 +サブルーチン内でのみ変更可能なカウンタです。 +これは、パッケージの生存期間中に変数が干渉されることがないように、とき毒 +パッケージファイルの BEGIN ブロックで使われます: BEGIN { my $id = 0; @@ -854,11 +851,10 @@ =end original -もし変数の自殺に遭遇したら、that C<my $f> in the subroutine -doesn't pick up a fresh copy of the C<$f> whose value is <foo>. The output -shows that inside the subroutine the value of C<$f> leaks through when it -shouldn't, as in this output: -(TBT) +もし変数の自殺に遭遇したら、サブルーチン内の C<my $f> は、値が C<foo> である +C<$f> の最新のコピーをではありません。 +この出力は、漏れてはいけないサブルーチン内の C<$f> の値が漏れていることを +示し、以下のようになります: foobar foobarbar @@ -1156,23 +1152,21 @@ =end original -You can fake a static variable by using a lexical variable which goes -out of scope. +スコープ外でレキシカル変数を使うことでスタティック変数を +でっち上げることができます。 この例では、サブルーチン C<counter> を定義し、そこでレキシカル変数 C<$count> を使います。 これを BEGIN ブロックで囲っているので、C<$count> はコンパイル時に 定義されますが、BEGIN ブロックの終わりでスコープから外れます。 -The BEGIN block also ensures that -the subroutine and the value it uses is defined at compile-time so the -subroutine is ready to use just like any other subroutine, and you can -put this code in the same place as other subroutines in the program -text (i.e. at the end of the code, typically). +BEGIN ブロックはまた、サブルーチンとそこで使われている値はコンパイル時に +定義されるので、このサブルーチンはその他のどのサブルーチンからも +使える準備が出来ていて、このコードをプログラムテキスト中の他のサブルーチンと +同様に同じ場所(典型的には、コードの最後)にこのコードを置けることを保証します。 サブルーチン C<counter> は未だにデータへのリファレンスを持っていて、 これが値にアクセスできる唯一の方法です(そしてそうする度に、値が インクリメントされます)。 -The data in chunk of memory defined by C<$count> is private to -C<counter>. -(TBT) +C<$count> で定義されたメモリの塊にあるデータは C<counter> に +プライベートです。 BEGIN { my $count = 1; @@ -1200,17 +1194,17 @@ =end original -In the previous example, you created a function-private variable -because only one function remembered its reference. You could define -multiple functions while the variable is in scope, and each function -can share the "private" variable. It's not really "static" because you -can access it outside the function while the lexical variable is in -scope, and even create references to it. In this example, -C<increment_count> and C<return_count> share the variable. One -function adds to the value and the other simply returns the value. -They can both access C<$count>, and since it has gone out of scope, -there is no other way to access it. -(TBT) +前述の例では、関数でプライベートな変数を作っています; +ただ一つの関数だけがこのリファレンスを覚えているからです。 +変数がスコープ内にある間に複数の関数を定義でき、各関数は「プライベート」 +変数を共有できます。 +レキシカル変数がスコープ内ではあるけれども関数の外側から変数にアクセスでき、 +それに対するリファレンスすら作成できるため、これは実際には +「静的」変数ではありません。 +この例では、C<increment_count> と C<return_count> は変数を共有しています。 +片方の関数が値を加え、もう片方は単に値を返します。 +これらは両方とも C<$count> にアクセスでき、スコープ外から離れるため、 +これにアクセスするその他の方法はありません。 BEGIN { my $count = 1; @@ -2206,16 +2200,13 @@ =end original -If you see "bad interpreter - no such file or directory", the first -line in your perl script (the "shebang" line) does not contain the -right path to perl (or any other program capable of running scripts). -Sometimes this happens when you move the script from one machine to -another and each machine has a different path to perl--/usr/bin/perl -versus /usr/local/bin/perl for instance. It may also indicate -that the source machine has CRLF line terminators and the -destination machine has LF only: the shell tries to find -/usr/bin/perl<CR>, but can't. -(TBT) +"bad interpreter - no such file or directory" と表示されたら、perl +スクリプトの最初の行 ("#!" 行) に perl (あるいはスクリプトを実行する +機能のあるその他のプログラム) への正しいパスが含まれていません。 +スクリプトをあるマシンから、perl のパスが異なる -- 例えば /usr/bin/perl と +/usr/local/bin/perl -- 他のマシンに移動させた場合に時々起こります。 +これはまた、元のマシンの行終端が CRLF で、移動先のマシンが LF のみの場合にも +起こります; シェルが /usr/bin/perl<CR> を探そうとしますが、失敗します。 =begin original