strict - 安全ではないコンストラクトを制限する Perl プラグマ
use strict;
use strict "vars";
use strict "refs";
use strict "subs";
use strict;
no strict "vars";
If no import list is supplied, all possible restrictions are assumed. (This is the safest mode to operate in, but is sometimes too strict for casual programming.) Currently, there are three possible things to be strict about: "subs", "vars", and "refs".
インポートリストを与えない場合は、可能な限り全ての制約を受けます。 (これは、最も安全な動作モードです。ただ、カジュアルプログラミング のためには厳しすぎます。)今のところ、"subs"、"vars"、"refs" の 3つの制約が用意されています。
strict refs
This generates a runtime error if you use symbolic references (see perlref).
シンボリックリファレンスが使われたときにランタイムエラーになります。 (perlref を見てください。)
use strict 'refs';
$ref = \$foo;
print $$ref; # ok
$ref = "foo";
print $$ref; # ランタイムエラー; 普段は ok
$file = "STDOUT";
print $file "Hi!"; # エラー; note: $file の後にコンマがない。
There is one exception to this rule:
このルールには 1つの例外があります。
$bar = \&{'foo'};
&$bar;
is allowed so that goto &$AUTOLOAD would not break under stricture.
上記のものは許容されます。だから goto &$AUTOLOAD はこの制約下でも
動きます。
strict vars
This generates a compile-time error if you access a variable that wasn't
declared via our or use vars,
localized via my(), or wasn't fully qualified. Because this is to avoid
variable suicide problems and subtle dynamic scoping issues, a merely
local() variable isn't good enough. See perlfunc/my and
perlfunc/local.
our や use vars、my() で宣言された変数や完全に修飾された
変数以外にアクセスしたときにコンパイル時エラーを出します。
変数が自殺してしまう問題や微表な動的スコープの問題があるため、
local() 変数だけでは十分ではありません。perlfunc/my や
perlfunc/local を見てください。
use strict 'vars';
$X::foo = 1; # ok, 完全に修飾されています
my $foo = 10; # ok, my() 変数
local $foo = 9; # ダメ
package Cinna;
our $bar; # パッケージ内で宣言された $bar
$bar = 'HgS'; # ok, プラグマでグローバルに宣言された
The local() generated a compile-time error because you just touched a global name without fully qualifying it.
local() は、完全な修飾無しにグローバルな名前を触ってしまうため コンパイル時エラーを出します。
Because of their special use by sort(), the variables $a and $b are exempted from this check.
sort() によって使われるという理由で $a と $b はこのチェックの 適用外という特別扱いになっています。
strict subs
This disables the poetry optimization, generating a compile-time error if
you try to use a bareword identifier that's not a subroutine, unless it
is a simple identifier (no colons) and that it appears in curly braces or
on the left hand side of the => symbol.
詩的な最適化を禁止し、サブルーチン以外の裸の識別子を使おうとしたときか、
(コロンのない)単純な識別子や中括弧の中 => シンボルの左側に
無いときにコンパイル時エラーを出します。
use strict 'subs';
$SIG{PIPE} = Plumber; # ダメ
$SIG{PIPE} = "Plumber"; # 問題なし: 中括弧の中ならいつでも裸で ok
$SIG{PIPE} = \&Plumber; # 好ましい方法
See perlmodlib/Pragmatic Modules.
perlmodlib/Pragmatic Modules を見てください。
strict 'subs', with Perl 5.6.1, erroneously permitted to use an unquoted
compound identifier (e.g. Foo::Bar) as a hash key (before => or
inside curlies), but without forcing it always to a literal string.
Perl 5.6.1 での strict 'subs' は、(=> の前や中括弧の中での)
ハッシュのキーのとしてクオートすることなしに(Foo::Bar のような)
複合の識別子を使えるようにしてしまっています。このことは間違いでした。
それは、いつでもリテラル文字列です。
Starting with Perl 5.8.1 strict is strict about its restrictions: if unknown restrictions are used, the strict pragma will abort with
Unknown 'strict' tag(s) '...'
Perl 5.8.1 からの strict は、それらの制約事項について厳格です: もし、知られていない制約事項が使われるならば、strict プラグマは、 次にある記述と共に中断するでしょう。
Unknown 'strict' tag(s) '...'