モジュールの最後の真値は不要に
この文書は PPCs/ppcs
/ppc0018-module-true.md を翻訳したものです。
原題は「No Longer Require a True Value at the End of a Module」です。
Preamble 序文
翻訳注:以下は原文のママです。
Author: Curtis "Ovid" Poe <curtis.poe@gmail.com>
Sponsor:
ID: 0018
Status: Draft
Abstract 要約
このPPC提案は、Perlのモジュールの末尾を伝統的に "1" や他の真の値で終わらせる必要をなくすものです。
Motivation 動機
Eliminate the need for a true value at the end of a Perl file.
Perlファイルの末尾を真値で終わらせる必要をなくします。
Rationale 提案理由
There's no need to have a true value be hard-coded in our files that we
`use`. Further, newer programmers can get confused because sometimes code
_doesn't_ end with a true value but nonetheless compiles just fine because
_something_ in the code returned a true value and the code compiles as a
side-effect.
use
するときに、ファイルに真値をハードコードする必要性はありません。さらに、新参者の
プログラマーは真値でコードが 終了しない にもかかわらず、真値を返す なにかしら が
副作用でコードをコンパイルできてしまうことで混乱します。
Specification 仕様
First, a new `feature` is added:
まず、新しい feature
が追加されます:
perl
use feature 'module_true';
Then, *whenever* a module is loaded with `require` (or an equivalent, like
`use`), the "croak if false" test is skipped if the `module_true` feature was
in effect at the last statement executed in the required module.
そうして、require
(あるいは同等の use
など)でモジュールがロードされた時は いつでも、requireされたモジュール内で実行されている最後のステートメントでmodule_true
フィーチャーが
有効ならば、"croak if false(偽ならcroakする)"テストをスキップします。
Backwards Compatibility 後方互換性
There are no compatibility concerns I'm aware of because we're only suggesting
changing behaviour in the presence of a newly-added feature that is not
present in any existing code.
私の気づく範囲で互換性の問題はありません。なぜなら既存のコードにはない新しいfeatureを追加して
振る舞いを変えることを提案しているにすぎないからです。
Security Implications セキュリティ懸念
None expected.
予想されるものはありません。
Examples
Imagine this module:
下記のモジュールを想像してください:
```perl
package Demo1;
use feature 'module_true';
sub import {
warn "You imported a module!\n";
}
```
When loaded by `require` or `use` anywhere in perl, this would import
successfully, despite the lack of a true value at the end.
This module shows an (almost certainly never useful) way to croak anyway:
perlのどこでもrequire
またはuse
によってロードされる時、このインポートは成功し、
末尾の真値がなくても関係ありません。
このモジュールは(ほとんど確実に役に立たない)croakする方法を示します。
```perl
package Demo2;
use feature 'module_true';
return 1 if $main::test_1;
return 0 if $main::test_2;
{
no feature 'module_true';
return 0 if $main::test_3;
}
```
In this example, the only case in which requiring Demo2 would fail is if
`$main::test_3` was true. The previous `return 0 if $main::test_2` would still
be within the scope of the `module_true` feature, so the return value would be
ignored. When `0` is returned outside the effect of `module_true`, though, the
old behavior of testing the return value is back in effect.
こちらの例では、Demo2のrequireが失敗する唯一のケースは、$main::test_3
が真のときです。
すぐ前の return 0 if $main::test_2
では、依然として module_true
feature のスコープの
範囲内なので、戻り値は無視されます。しかし、module_true
の効果の範囲外で 0
が返される時、
古い振る舞いである戻り値のチェック機能が復帰します。
Prototype Implementation 試験実装
There is a prototype implementation at [true](https://metacpan.org/pod/true).
試験実装はこちらです。 [true](https://metacpan.org/pod/true)
3752a6a2a369b04e1338bc35f50f58c1
Due to this being a named feature, this can eventually be the default behavior
when `use v5.XX;` is used.
名前付きfeatureになるので、use v5.XX;
が使用された時のデフォルトの挙動に含まれることになります。
Rejected Ideas 却下された案
It's been discussed that we should return the package name instead. This
supports:
戻り値にパッケージ名を返すべきかどうかが議論されました。これは以下をサポートします。
perl
my $obj = (require Some::Object::Class)->new;
However, per haarg:
しかしながら、haag曰く:
8e94c4f54aca4e5f68658816a966106d