=encoding euc-jp =head1 NAME =begin original constant - Perl pragma to declare constants =end original constant - 定数を宣言するための Perl プラグマ =head1 SYNOPSIS use constant PI => 4 * atan2(1, 1); use constant DEBUG => 0; print "Pi equals ", PI, "...\n" if DEBUG; use constant { SEC => 0, MIN => 1, HOUR => 2, MDAY => 3, MON => 4, YEAR => 5, WDAY => 6, YDAY => 7, ISDST => 8, }; use constant WEEKDAYS => qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ); print "Today is ", (WEEKDAYS)[ (localtime)[WDAY] ], ".\n"; =head1 DESCRIPTION =begin original This pragma allows you to declare constants at compile-time. =end original このプラグマは、コンパイル時に定数を宣言できるようにします。 =begin original When you declare a constant such as C using the method shown above, each machine your script runs upon can have as many digits of accuracy as it can use. Also, your program will be easier to read, more likely to be maintained (and maintained correctly), and far less likely to send a space probe to the wrong planet because nobody noticed the one equation in which you wrote C<3.14195>. =end original 上述の C のようにメソッドを使った定数を宣言すると、マシン毎に 利用可能なだけの精度でスクリプトを実行します。 さらに、プログラムが読みやすくなり、それどころか保守しやすく (そして正しく保守しやすく)なり、C<3.14195> と書いた一つの方程式に 誰も気付かなかったために宇宙探査機を間違った星に送り込む可能性が 遥かに小さくなります。 =begin original When a constant is used in an expression, Perl replaces it with its value at compile time, and may then optimize the expression further. In particular, any code in an C block will be optimized away if the constant is false. =end original 定数を式で使ったとき、Perl はこれをコンパイル時に値に置き換え、それから さらに式を最適化します。 特に、C ブロックにあるコードは、定数が偽の時には最適化されて なくなります。 =head1 NOTES (注意) =begin original As with all C directives, defining a constant happens at compile time. Thus, it's probably not correct to put a constant declaration inside of a conditional statement (like C). =end original 全ての C 指示子と同様、定数の定義はコンパイル時に行われます。 従って、(C のように) 条件文の内側で定数宣言を 行うのはおそらく間違っています。 =begin original Constants defined using this module cannot be interpolated into strings like variables. However, concatenation works just fine: =end original このモジュールを使って定義された定数は変数のように文字列に展開されません。 しかし、結合はうまく動きます: print "Pi equals PI...\n"; # WRONG: does not expand "PI" print "Pi equals ".PI."...\n"; # right =begin original Even though a reference may be declared as a constant, the reference may point to data which may be changed, as this code shows. =end original リファレンスを定数として宣言することはできますが、以下のコードが示すように リファレンスは変更されるデータを指しているかもしれません。 use constant ARRAY => [ 1,2,3,4 ]; print ARRAY->[1]; ARRAY->[1] = " be changed"; print ARRAY->[1]; =begin original Dereferencing constant references incorrectly (such as using an array subscript on a constant hash reference, or vice versa) will be trapped at compile time. =end original (定数ハッシュリファレンスに配列添え字を使ったり、その逆のように) 定数リファレンスを間違ってデリファレンスすると、コンパイル時に トラップされます。 =begin original Constants belong to the package they are defined in. To refer to a constant defined in another package, specify the full package name, as in C. Constants may be exported by modules, and may also be called as either class or instance methods, that is, as C<< Some::Package->CONSTANT >> or as C<< $obj->CONSTANT >> where C<$obj> is an instance of C. Subclasses may define their own constants to override those in their base class. =end original 定数は定義されたパッケージに属します。 他のパッケージで定義された定数を参照するには、 C のように完全なパッケージ名を指定します。 定数はモジュールによってエクスポートすることができ、またクラスメソッド またはインスタンスメソッドとして呼び出すこともできます; C<$obj> が C のインスタンスのときに C<< Some::Package->CONSTANT >> や C<< $obj->CONSTANT >> のようにできます。 サブクラスは基底クラスをオーバーライドして独自の定数を定義できます。 =begin original The use of all caps for constant names is merely a convention, although it is recommended in order to make constants stand out and to help avoid collisions with other barewords, keywords, and subroutine names. Constant names must begin with a letter or underscore. Names beginning with a double underscore are reserved. Some poor choices for names will generate warnings, if warnings are enabled at compile time. =end original 定数名に全て大文字を使うのは単に慣習ですが、定数を目立つようにして 他の裸の単語、キーワード、サブルーチン名との衝突を避けることを助けるために、 そのようにすることを勧めます。 定数名は英字または下線で始まらなければなりません。 下線二つで始まる名前は予約されています。 警告がコンパイル時に有効になっているなら、間違った名前を選択すると 警告が生成されます。 =head2 List constants (リスト定数) =begin original Constants may be lists of more (or less) than one value. A constant with no values evaluates to C in scalar context. Note that constants with more than one value do I return their last value in scalar context as one might expect. They currently return the number of values, but B. Do not use constants with multiple values in scalar context. =end original 定数は一つより多い(または少ない)値のリストを指定できます。 値のない定数はスカラコンテキストでは C と評価されます。 複数の値を持つ定数は、想定されるようにスカラコンテキストで最後の値を 返す I<わけではない> ことに注意してください。 これらは現在のところ値の数を返しますが、 B<これは将来変更されるかもしれません>。 複数の値を持つ定数はスカラコンテキストで使わないでください。 =begin original B This implies that the expression defining the value of a constant is evaluated in list context. This may produce surprises: =end original B<注意:> これは定数の値をリストコンテキストで評価された値を定義する 式です。 これは驚きを呼ぶかもしれません: use constant TIMESTAMP => localtime; # WRONG! use constant TIMESTAMP => scalar localtime; # right =begin original The first line above defines C as a 9-element list, as returned by C in list context. To set it to the string returned by C in scalar context, an explicit C keyword is required. =end original 上述の 1 番目の行は、C をリストコンテキストで C から 返される 9 要素リストとして定義します。 スカラコンテキストで C から返される文字列にするには、明示的な C キーワードが必要です。 =begin original List constants are lists, not arrays. To index or slice them, they must be placed in parentheses. =end original リスト定数は配列ではなくリストです。 インデックスやスライスを使うには、かっこの中に書かなければなりません。 my @workdays = WEEKDAYS[1 .. 5]; # WRONG! my @workdays = (WEEKDAYS)[1 .. 5]; # right =head2 Defining multiple constants at once (一度に複数の定数を定義する) =begin original Instead of writing multiple C statements, you may define multiple constants in a single statement by giving, instead of the constant name, a reference to a hash where the keys are the names of the constants to be defined. Obviously, all constants defined using this method must have a single value. =end original C 文を複数回書く代わりに、一つの文で複数の定数を定義できます; 定数名の代わりに、キーが定数名であるハッシュへのリファレンスを渡します。 明らかに、この手法を使って定義された定数は単一の値を持たなければなりません。 use constant { FOO => "A single value", BAR => "This", "won't", "work!", # Error! }; =begin original This is a fundamental limitation of the way hashes are constructed in Perl. The error messages produced when this happens will often be quite cryptic -- in the worst case there may be none at all, and you'll only later find that something is broken. =end original これは Perl でハッシュを構築するための基本的な制限です。 これが起きたときのエラーメッセージはしばしばかなり暗号的です -- 最悪の場合、 全くメッセージは出ず、後で何かが壊れていることに気付くだけです。 =begin original When defining multiple constants, you cannot use the values of other constants defined in the same declaration. This is because the calling package doesn't know about any constant within that group until I the C statement is finished. =end original 複数の定数を定義するとき、同じ宣言で定義したその他の定数の値は使えません。 これは呼び出しパッケージは C 文が完了した I<後> でしかグループの内側の 定数に関して知ることがないからです。 use constant { BITMASK => 0xAFBAEBA8, NEGMASK => ~BITMASK, # Error! }; =head2 Magic constants (マジック定数) =begin original Magical values and references can be made into constants at compile time, allowing for way cool stuff like this. (These error numbers aren't totally portable, alas.) =end original マジック変数とリファレンスはコンパイル時に定数にすることができ、 以下のようなかっこいいことができるようになります。 (これらのエラー番号は完全な移植性はありません; あーあ。) use constant E2BIG => ($! = 7); print E2BIG, "\n"; # something like "Arg list too long" print 0+E2BIG, "\n"; # "7" =begin original You can't produce a tied constant by giving a tied scalar as the value. References to tied variables, however, can be used as constants without any problems. =end original 値として tie されたスカラを与えることで tie された定数を 生成することはできません。 しかし、tie された変数へのリファレンスは、何の問題もなく定数として 使えます。 =head1 TECHNICAL NOTES (技術的な注意) =begin original In the current implementation, scalar constants are actually inlinable subroutines. As of version 5.004 of Perl, the appropriate scalar constant is inserted directly in place of some subroutine calls, thereby saving the overhead of a subroutine call. See L for details about how and when this happens. =end original 現在の実装では、スカラ定数は実際にはインライン化可能なサブルーチンです。 Perl バージョン 5.004 から、対応するスカラ定数はサブルーチン呼び出しの 場所に直接挿入されるので、サブルーチン呼び出しのオーバーヘッドが なくなります。 これがいつどのように起こるかの詳細については L を参照してください。 =begin original In the rare case in which you need to discover at run time whether a particular constant has been declared via this module, you may use this function to examine the hash C<%constant::declared>. If the given constant name does not include a package name, the current package is used. =end original 稀な場合として、ある定数がこのモジュール経由で宣言されたかどうかを 実行時に発見する必要があるなら、ハッシュ C<%constant::declared> を 調べるために以下の関数が使えます。 与えられた定数名にパッケージ名が含まれていない場合、現在のパッケージが 使われます。 sub declared ($) { use constant 1.01; # don't omit this! my $name = shift; $name =~ s/^::/main::/; my $pkg = caller; my $full_name = $name =~ /::/ ? $name : "${pkg}::$name"; $constant::declared{$full_name}; } =head1 CAVEATS (警告) =begin original In the current version of Perl, list constants are not inlined and some symbols may be redefined without generating a warning. =end original 現在のバージョンの Perl では、リスト定数はインライン化されず、一部の シンボルは警告を出さずに再定義できてしまいます。 =begin original It is not possible to have a subroutine or a keyword with the same name as a constant in the same package. This is probably a Good Thing. =end original 同じパッケージで定数と同じ名前のサブルーチンやキーワードは使えません。 これはおそらく「良いこと」です。 =begin original A constant with a name in the list C is not allowed anywhere but in package C, for technical reasons. =end original 技術的な理由により、C という名前の定数は C パッケージ以外では 宣言できません。 =begin original Unlike constants in some languages, these cannot be overridden on the command line or via environment variables. =end original 一部の言語の定数と異なり、コマンドラインや環境定数で定数を 上書きできません。 =begin original You can get into trouble if you use constants in a context which automatically quotes barewords (as is true for any subroutine call). For example, you can't say C<$hash{CONSTANT}> because C will be interpreted as a string. Use C<$hash{CONSTANT()}> or C<$hash{+CONSTANT}> to prevent the bareword quoting mechanism from kicking in. Similarly, since the C<< => >> operator quotes a bareword immediately to its left, you have to say C<< CONSTANT() => 'value' >> (or simply use a comma in place of the big arrow) instead of C<< CONSTANT => 'value' >>. =end original 自動的に裸の単語をクォートするコンテキストで定数を使うと (サブルーチン呼び出しの場合と同様)問題が起こります。 例えば、C<$hash{CONSTANT}> とはできません; C は文字列として 解釈されるからです。 裸の単語をクォートする機構を防ぐために C<$hash{CONSTANT()}> や C<$hash{+CONSTANT}> を使ってください。 同様に、the C<< => >> 演算子は左側の裸の単語をクォートするので、 C<< CONSTANT => 'value' >> ではなく C<< CONSTANT() => 'value' >> とする (または単に大矢印の代わりにカンマを使う)必要があります。 =head1 SEE ALSO =begin original L - Facility for creating read-only scalars, arrays, hashes. =end original L - 読み込み専用のスカラ、配列、ハッシュを作る機能。 =begin original L - Facility for creating read-only variables. Similar to C, but uses C instead of C. =end original L - 読み込み専用の変数を作る機能。 C と似ていますが、C ではなく C を使います。 =begin original L - Make read-only variables via attribute =end original L - 属性経由で読み込み専用変数を作る。 =begin original L - Perl extension to the C scalar flag =end original L - C スカラフラグのための Perl エクステンション =begin original L - A selection of general-utility hash subroutines (mostly to lock/unlock keys and values) =end original L - 汎用ハッシュサブルーチンの集合 (ほとんどはキーと値を ロック/アンロックするもの) =head1 BUGS =begin original Please report any bugs or feature requests via the perlbug(1) utility. =end original バグや機能追加リクエストは perlbug(1) ユーティリティを使って送って下さい。 =head1 AUTHORS =begin original Tom Phoenix, EFE, with help from many other folks. =end original Tom Phoenix, EFE が多くの人々の助けと共に。 =begin original Multiple constant declarations at once added by Casey West, EFE. =end original 一度に複数の定数を宣言する機能は Casey West, EFE によって追加されました。 =begin original Documentation mostly rewritten by Ilmari Karonen, EFE. =end original 文書は Ilmari Karonen, EFE によって ほとんど書き直されました。 =begin original This program is maintained by the Perl 5 Porters. The CPAN distribution is maintained by SEbastien Aperghis-Tramoni EFE. =end original このプログラムは the Perl 5 Porters によって保守されています。 CPAN 配布は SEbastien Aperghis-Tramoni EFE によって保守されています。 =head1 COPYRIGHT & LICENSE Copyright (C) 1997, 1999 Tom Phoenix This module is free software; you can redistribute it or modify it under the same terms as Perl itself. =begin meta Translate: SHIRAKATA Kentaro Status: completed =end meta =cut