=encoding euc-jp =head1 NAME =begin original perltrap - Perl traps for the unwary =end original perltrap - 不注意による Perl の罠 =head1 DESCRIPTION =begin original The biggest trap of all is forgetting to C or use the B<-w> switch; see L and L. The second biggest trap is not making your entire program runnable under C. The third biggest trap is not reading the list of changes in this version of Perl; see L. =end original 最も大きな罠とは、C あるいは B<-w> スイッチを使うのを 忘れてしまうということです; L と L を 参照してください。 二番目に大きな罠とは、あなたのプログラム全体を C の元で 実行しないということです。 三番目の罠は、このバージョンの Perl での変更点を読まないということです; L を参照してください。 =head2 Awk Traps (awk の罠) =begin original Accustomed B users should take special note of the following: =end original B に慣れた方は、以下のようなことに特に注意してください: =over 4 =item * =begin original A Perl program executes only once, not once for each input line. You can do an implicit loop with C<-n> or C<-p>. =end original Perl のプログラムは、1 度だけ実行されます; 入力行毎ではありません。 C<-n> や C<-p> を使って暗黙のループを使えます。 =item * =begin original The English module, loaded via =end original Englishモジュールを use English; =begin original allows you to refer to special variables (like C<$/>) with names (like $RS), as though they were in B; see L for details. =end original のようにしてロードすれば、B でそうであったように (C<$/> のような)特殊変数を($RS のような)名前で参照することができます; 詳しくは L を参照してください。 =item * =begin original Semicolons are required after all simple statements in Perl (except at the end of a block). Newline is not a statement delimiter. =end original Perl では、すべての単純文(simple statement)の末尾にセミコロンが必要です (ブロックの最後に置かれたときを除きます)。 改行は文の区切りとはなりません。 =item * =begin original Curly brackets are required on Cs and Cs. =end original C や C では中かっこが必要です。 =item * =begin original Variables begin with "$", "@" or "%" in Perl. =end original Perl では、変数は "$" か "@" か "%" で始まります。 =item * =begin original Arrays index from 0. Likewise string positions in substr() and index(). =end original 配列の添え字は 0 から始まります。 substr() や index() での文字列の位置も同様です。 =item * =begin original You have to decide whether your array has numeric or string indices. =end original 配列の添え字が数値であるか、文字列であるかを決めなければなりません。 =item * =begin original Hash values do not spring into existence upon mere reference. =end original ハッシュ(連想配列)の値は、単に参照するだけでは存在することになりません。 =item * =begin original You have to decide whether you want to use string or numeric comparisons. =end original 比較を文字列によって行うのか、数値によって行うのかを 決めなければなりません。 =item * =begin original Reading an input line does not split it for you. You get to split it to an array yourself. And the split() operator has different arguments than B's. =end original 入力を読み込むだけでは split は行われません。 配列への split は自分で行います。 また、split() 演算子の引数は B のものと異なっています。 =item * =begin original The current input line is normally in $_, not $0. It generally does not have the newline stripped. ($0 is the name of the program executed.) See L. =end original 通常、カレント行は $0 ではなく $_ にあります。 一般的に、改行は取り除かれません。 ($0 には実行しているプログラムの名前があります)。 L を参照してください。 =item * =begin original $> does not refer to fields--it refers to substrings matched by the last match pattern. =end original $> はフィールドを参照しません--これは直前に行った パターンマッチングの部分文字列を参照します。 =item * =begin original The print() statement does not add field and record separators unless you set C<$,> and C<$\>. You can set $OFS and $ORS if you're using the English module. =end original print() 文は、C<$,> や C<$\> に値を設定しない限りフィールド区切り子や レコード区切り子を付加しません。 English モジュールを使っていれば、$OFS や $ORS に対して 設定することもできます。 =item * =begin original You must open your files before you print to them. =end original ファイルに対して出力する前には、そのファイルをあらかじめオープンして おかなければなりません。 =item * =begin original The range operator is "..", not comma. The comma operator works as in C. =end original 範囲演算子は ".." であって、カンマではありません。 カンマ演算子は C と同じような振る舞いをします。 =item * =begin original The match operator is "=~", not "~". ("~" is the one's complement operator, as in C.) =end original マッチ演算子は "=~" であって、"~" ではありません。 ("~" はCと同様に、1 の補数を取る演算子です。) =item * =begin original The exponentiation operator is "**", not "^". "^" is the XOR operator, as in C. (You know, one could get the feeling that B is basically incompatible with C.) =end original べき乗の演算子は "**" であって、"^" ではありません。 "^" は C と同様、XOR 演算子です。 (B が基本的に C と非互換であることにお気付きかもしれませんね。) =item * =begin original The concatenation operator is ".", not the null string. (Using the null string would render C unparsable, because the third slash would be interpreted as a division operator--the tokenizer is in fact slightly context sensitive for operators like "/", "?", and ">". And in fact, "." itself can be the beginning of a number.) =end original 連接演算子は "." であって、空文字列ではありません。 (空文字列を使ってしまうと C が、その 3 番目のスラッシュが 除算演算子と解釈されてしまうので正しく解析できなくなります-- Perl の字句解析器は "/", "?", ">" といった演算子に対して 多少文脈依存となっています。 実際、"." 自身も数値の始まりとなる可能性もあります。) =item * =begin original The C, C, and C keywords work differently. =end original キーワード C, C, C の振る舞いが異なります。 =item * =begin original The following variables work differently: =end original 以下の変数の働きが異なります。 Awk Perl ARGC scalar @ARGV (compare with $#ARGV) ARGV[0] $0 FILENAME $ARGV FNR $. - something FS (whatever you like) NF $#Fld, or some such NR $. OFMT $# OFS $, ORS $\ RLENGTH length($&) RS $/ RSTART length($`) SUBSEP $; =item * =begin original You cannot set $RS to a pattern, only a string. =end original $RS に正規表現をセットすることはできません; できるのは文字列だけです。 =item * =begin original When in doubt, run the B construct through B and see what it gives you. =end original 妙だと思ったときには B の構文を B に通して、出力されたものを 見てみましょう。 =back =head2 C/C++ Traps (C/++ の罠) =begin original Cerebral C and C++ programmers should take note of the following: =end original 知的な C と C++ のプログラマは以下のことに注意すべきです: =over 4 =item * =begin original Curly brackets are required on C's and C's. =end original C や C には中かっこが必要です。 =item * =begin original You must use C rather than C. =end original C ではなく、C を使わなければなりません。 =item * =begin original The C and C keywords from C become in Perl C and C, respectively. Unlike in C, these do I work within a C construct. See L. =end original C の C と C は、Perl ではそれぞれ C と C となります。 C とは異なり、これらは C 構文では I<使えません>。 L を参照してください。 =item * =begin original The switch statement is called C/C and only available in perl 5.10 or newer. See L. =end original switch 文は C/C と呼ばれ、perl 5.10 以降でのみ利用可能です。 L を参照してください。 =item * =begin original Variables begin with "$", "@" or "%" in Perl. =end original Perl では、変数は "$" か "@" か "%" で始まります。 =item * =begin original Comments begin with "#", not "/*" or "//". Perl may interpret C/C++ comments as division operators, unterminated regular expressions or the defined-or operator. =end original コメントの始まりは、"#" であり、"/*" や "//" ではありません。 Perl は C/C++ のコメントを除算演算子、終端していない正規表現、 定義性和演算子として解釈するかもしれません。 =item * =begin original You can't take the address of anything, although a similar operator in Perl is the backslash, which creates a reference. =end original なにかのアドレスを得ることはできません; Perl には似たような演算子である バックスラッシュがありますが、これはリファレンスを生成します。 =item * =begin original C must be capitalized. C<$ARGV[0]> is C's C, and C ends up in C<$0>. =end original C は大文字でなければなりません。 C<$ARGV[0]> が C での C に相当し、C にあたるものは C<$0> です。 =item * =begin original System calls such as link(), unlink(), rename(), etc. return nonzero for success, not 0. (system(), however, returns zero for success.) =end original link(), unlink(), rename() などのシステムコールは、成功時に 0 ではなく非 0 の値を返します。 (但し、system() は成功時に 0 を返します。) =item * =begin original Signal handlers deal with signal names, not numbers. Use C to find their names on your system. =end original シグナルハンドラは、シグナル番号ではなくシグナル名を扱います。 使用できるシグナル名は、kill -l として確かめてください。 =back =head2 JavaScript Traps (JavaScript の罠) =begin original Judicious JavaScript programmers should take note of the following: =end original 思慮深い JavaScript プログラマは以下のことに注意すべきです: =over 4 =item * =begin original In Perl, binary C<+> is always addition. C<$string1 + $string2> converts both strings to numbers and then adds them. To concatenate two strings, use the C<.> operator. =end original Perl では、二項の C<+> は常に加算です。 C<$string1 + $string2> は両方の文字列を数値に変換してから加算します。 二つの文字列を結合するには、C<.> 演算子を使ってください。 =item * =begin original The C<+> unary operator doesn't do anything in Perl. It exists to avoid syntactic ambiguities. =end original C<+> 単項演算子は Perl では何もしません。 これは文法的な曖昧さを避けるために存在しています。 =item * =begin original Unlike C, Perl's C (also spelled C) does not allow the left-hand side to be an arbitrary expression. It must be a variable: =end original C と異なり、Perl の C (C と書くこともあります) は 左側に任意の式を置くことはできません。 これは変数でなければなりません: for my $variable (keys %hash) { ... } =begin original Furthermore, don't forget the C in there, as C iterates over the keys and values, and is generally not useful ($kv would be a key, then a value, and so on). =end original さらに、ここで C を忘れないでください; C は キーと値に対して反復するので、一般的には有用ではありません ($kv はキーになり、 次に値になり、という形になります)。 =item * =begin original To iterate over the indices of an array, use C. C iterates over the values. =end original 配列のインデックスに対して反復するには、 C を使ってください。 C は値に対して反復します。 =item * =begin original Perl requires braces following C, C, C, etc. =end original Perl は C, C, C などに引き続いて中かっこが必要です。 =item * =begin original In Perl, C is spelled C. =end original Perl では、C は C と書きます。 =item * =begin original C has higher precedence than assignment. In JavaScript, one can write: =end original C は代入より高い優先順位を持ちます。 JavaScript では、以下のように書けて: condition ? do_something() : variable = 3 =begin original and the variable is only assigned if the condition is false. In Perl, you need parentheses: =end original 条件が偽の場合にのみ変数に代入されます。 Perl では、かっこが必要です: $condition ? do_something() : ($variable = 3); =begin original Or just use C. =end original または単に C を使ってください。 =item * =begin original Perl requires semicolons to separate statements. =end original Perl は文の分割にセミコロンが必要です。 =item * =begin original Variables declared with C only affect code I the declaration. You cannot write C<$x = 1; my $x;> and expect the first assignment to affect the same variable. It will instead assign to an C<$x> declared previously in an outer scope, or to a global variable. =end original C で宣言された変数は宣言の I<後> でのみ効果を持ちます。 C<$x = 1; my $x;> と書いて最初の代入が同じ変数に影響すると 想定することはできません。 これは外側のスコープ、あるいはグローバル変数の C<$x> に代入されます。 =begin original Note also that the variable is not visible until the following I. This means that in C the second $x refers to one declared previously. =end original また、変数は次の I<文> まで有効にならないことにも注意してください。 これは、C とすると 2 番目の $x は前に宣言されたものを 参照するということです。 =item * =begin original C variables are scoped to the current block, not to the current function. If you write C<{my $x;} $x;>, the second C<$x> does not refer to the one declared inside the block. =end original C 変数のスコープは現在の関数ではなく現在のブロックです。 C<{my $x;} $x;> と書くと、2 番目の C<$x> はブロックの内側で宣言された変数を 参照できません。 =item * =begin original An object's members cannot be made accessible as variables. The closest Perl equivalent to C is C, which can alias C<$_> to the object: =end original オブジェクトのメンバは変数としてアクセスできません。 C の Perl での一番近い等価物は C で、 これは C<$_> がオブジェクトへの別名となります: for ($object) { $_->method; } =item * =begin original The object or class on which a method is called is passed as one of the method's arguments, not as a separate C value. =end original メソッドが呼び出されたオブジェクトまたはクラスは、独立した C 値ではなく メソッドの引数の一つとして渡されます。 =back =head2 Sed Traps (sed の罠) =begin original Seasoned B programmers should take note of the following: =end original 熟練した B プログラマは以下のことに注意すべきです: =over 4 =item * =begin original A Perl program executes only once, not once for each input line. You can do an implicit loop with C<-n> or C<-p>. =end original Perl のプログラムは、1 度だけ実行されます; 入力行毎ではありません。 C<-n> や C<-p> を使って暗黙のループを使えます。 =item * =begin original Backreferences in substitutions use "$" rather than "\". =end original 置換における後方参照には、"\" ではなく "$" を使います。 =item * =begin original The pattern matching metacharacters "(", ")", and "|" do not have backslashes in front. =end original "(", ")", "|" といったパターンマッチのメタキャラクタは、その直前に バックスラッシュを置く必要はありません。 =item * =begin original The range operator is C<...>, rather than comma. =end original 範囲演算子は C<...> であって、カンマではありません。 =back =head2 Shell Traps (shell の罠) =begin original Sharp shell programmers should take note of the following: =end original 鋭いシェルプログラマは以下のことに注意すべきです: =over 4 =item * =begin original The backtick operator does variable interpolation without regard to the presence of single quotes in the command. =end original バッククォート演算子は、コマンド内にシングルクォートがあっても 変数の展開を行ないます。 =item * =begin original The backtick operator does no translation of the return value, unlike B. =end original バッククォート演算子は B とは違って、返された値を変換しません。 =item * =begin original Shells (especially B) do several levels of substitution on each command line. Perl does substitution in only certain constructs such as double quotes, backticks, angle brackets, and search patterns. =end original シェル (特に B) は、コマンドラインごとに何段階もの置換を行ないます。 Perl はダブルクォート、バッククォート、山かっこ、検索パターンといった 特定の構造でだけ置換を行ないます。 =item * =begin original Shells interpret scripts a little bit at a time. Perl compiles the entire program before executing it (except for C blocks, which execute at compile time). =end original シェルは一度に少しずつ解釈を行ないます。 Perl は実行前にプログラム全体をコンパイルします (コンパイル時に実行される C ブロックを除く)。 =item * =begin original The arguments are available via @ARGV, not $1, $2, etc. =end original 引数は $1, $2 などではなく、@ARGV から得られます。 =item * =begin original The environment is not automatically made available as separate scalar variables. =end original 環境変数は、自動的には独立したスカラ変数として利用できるようになりません。 =item * =begin original The shell's C uses "=", "!=", "<" etc for string comparisons and "-eq", "-ne", "-lt" etc for numeric comparisons. This is the reverse of Perl, which uses C, C, C for string comparisons, and C<==>, C C<< < >> etc for numeric comparisons. =end original シェルの C は "=", "!=", "<" などを文字列比較に使い、"-eq", "-ne", "-lt" などを数値比較に使います。 これは Perl とは逆です; Perl では C, C, C を文字列比較に使い、 C<==>, C C<< < >> などを数値比較に使います。 =back =head2 Perl Traps (Perl の罠) =begin original Practicing Perl Programmers should take note of the following: =end original 実践的な Perl プログラマは以下のことに注意すべきです: =over 4 =item * =begin original Remember that many operations behave differently in a list context than they do in a scalar one. See L for details. =end original 多くの演算子がリストコンテキストとスカラコンテキストとで 振る舞いが変わることを忘れないでください。 詳しくは L を参照してください。 =item * =begin original Avoid barewords if you can, especially all lowercase ones. You can't tell by just looking at it whether a bareword is a function or a string. By using quotes on strings and parentheses on function calls, you won't ever get them confused. =end original 裸の単語、特に全てが小文字のものはできる限り使わないでください。 見た目だけではその「裸の単語」が関数なのか、 文字列なのかが判断できません。 文字列にはクォートを、関数呼び出しには括弧をつければ、 迷うこともないでしょう。 =item * =begin original You cannot discern from mere inspection which builtins are unary operators (like chop() and chdir()) and which are list operators (like print() and unlink()). (Unless prototyped, user-defined subroutines can B be list operators, never unary ones.) See L and L. =end original 組込み関数のどれが(chop() や chdir())のような単項演算子で、 どれが(print() や unlink())のような リスト演算子であるかは見ただけではわかりません。 (プロトタイプがなければ、ユーザー定義サブルーチンは リスト演算子として B<のみ> 定義でき、単項演算子にはできません。) L と L を参照してください。 =item * =begin original People have a hard time remembering that some functions default to $_, or @ARGV, or whatever, but that others which you might expect to do not. =end original いくつかの関数が $_ や @ARGV などをデフォルトにしていますが、 同じことを期待する他の関数がデフォルトになっていないことを覚えるのに、 辛いタイピングが必要でしょう。 =item * =begin original The construct is not the name of the filehandle, it is a readline operation on that handle. The data read is assigned to $_ only if the file read is the sole condition in a while loop: =end original 構造はファイルハンドルではなく、そのハンドルに対する行読み込みの 操作(readline operation)です。 while ループの条件式の中にこのファイル読み込みだけがあった場合には 読み込まれたデータは $_ に代入されます。 while () { } while (defined($_ = )) { }.. ; # data discarded! =item * =begin original Remember not to use C<=> when you need C<=~>; these two constructs are quite different: =end original C<=~> が必要なところで C<=> を使わない、ということを忘れないでください; これら二つの構造はかなり違います: $x = /foo/; $x =~ /foo/; =item * =begin original The C construct isn't a real loop that you can use loop control on. =end original C 構造は、ループ制御を行えるような本当のループではありません。 =item * =begin original Use C for local variables whenever you can get away with it (but see L for where you can't). Using C actually gives a local value to a global variable, which leaves you open to unforeseen side-effects of dynamic scoping. =end original ローカル変数は、C で済むところではこれで済ませてください (使えない場所については、L を参照してください)。 C を使えばグローバル変数に対するローカルな値を与えますが、 動的スコープの不慮の副作用の可能性は、そのままです。 =item * =begin original If you localize an exported variable in a module, its exported value will not change. The local name becomes an alias to a new value but the external name is still an alias for the original. =end original モジュールにある export された変数を局所化すると、その export された 値は変更されません。 ローカル名は新しい値の別名(alias)となりますが、 外部名は元々の値の別名のままです。 =back =begin original As always, if any of these are ever officially declared as bugs, they'll be fixed and removed. =end original いつものように、バグとして公式に宣言されたものがあれば、 それは修正されて取り除かれるでしょう。 =begin meta Translate: KIMURA Koichi (5.005_03) Update: SHIRAKATA Kentaro (5.6.1-) Status: completed =end meta