Pyon's Diary
2008-08-21 舊 平成廿年戊子文月廿壹日癸巳 (木・晴) [長年日記]
perl-support.vim (3.8.1) に乘換へた。
今迄文法檢査に
を使ひ、整形には$HOME/.vimrcに
"----------------------------------------------------------------------------- " Perltidy (Perl Hacks #7) " http://perltidy.sourceforge.net/ " map ,pt <ESC>:%! perltidy<CR> map ,ptv <ESC>:%'<, '>! perltidy<CR>
を追加してPerltidyを使つてゐたが、
では其乃兩方がサポートされてゐる他、カーソルの在る單語をperldocで引ける等、機能が滿載なので其方に乘換へる事にした。
今迄使つてゐた機能は、
\rs -- 文法檢査。 \ry -- Perltidy
で出來た。
help perlsupport-mappings
で各種機能のキー・バインドが判る。
今時のPerl (5.8.8)では「use constant」では無くて「use Readonly」らしい。
を讀んでゐて其乃樣に書いて在つたので、檢索してみると時代は「use Readonly」らしい。
を讀んで試してみた。
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use Data::Dumper;
my $a = "Hello";
print Dumper($a);
Internals::SvREADONLY( $a, 1 );
eval { $a = "World" };
$@ and carp $@;
print Dumper($a);
Internals::SvREADONLY( $a, 0 );
eval { $a = "Japan" };
$@ and carp $@;
print Dumper($a);
實行して見ると以下の樣に成つた。
% perl test.pl $VAR1 = 'Hello'; Modification of a read-only value attempted at test.pl line 12. at test.pl line 13 $VAR1 = 'Hello'; $VAR1 = 'Japan';
慥かに變數が讀出専用に成つてゐた。へぇ〜。
今時のPerl (5.8.8)ではサブルーチン呼出の「&」(アンパサンド)は不要らしい。
perlcritic (1.082)で自分の書いたスクリプトを檢査してみたら、
% perlcritic -2 test.pl (中略) Subroutine called with "&" sigil at line 26, column 36. See page 175 of PBP. (Severity: 2) (以下略)
とか云はれた。自分の記憶が確かならばサブルーチン呼出の場合は寧ろアンパサンドを頭に附けた方が良い、と思つてゐたのだが今時は違ふらしい。
上のメッセージで檢索して見たら、其乃事を解説してゐる頁を見附けた。
此乃頁でサブルーチン呼出にアンパサンドを使ふ樣に成つた歴史的經緯と今となつては何故駄目かが解説されてゐた。
何故Perl (5.x)では良くないかと云ふと
The old style &foo does not support prototypes, but you can still call subroutines that have prototypes with it. The prototype is ignored. That is potentially dangerous, because the sub might count on the prototype doing things for it. Also, it makes the ampersand form disable optimizations like the inlining of constants.
[Subroutines called with the ampersand / Perl 5 Wikiより引用]
と書いて在る樣にアンパサンドを附けて呼出すとプロトタイプ宣言が無視されるとの事。
實際に實驗してみた。
% cat test.pl
#!/usr/bin/perl
use strict;
use warnings;
sub hello($$) {
my (@args) = @_;
print join(':', @args),"\n";
}
&hello('a');
&hello('b', 'c');
&hello('d', 'e', 'f');
hello('g');
hello('h', 'i');
hello('j', 'k', 'l');
% perl test.pl
Not enough arguments for main::hello at test.pl line 14, near "'g')"
Too many arguments for main::hello at test.pl line 16, near "'l')"
Execution of test.pl aborted due to compilation errors.
と成つて慥かにアンパサンドを附けるとプロトタイプ宣言が無視された。アンパサンドを附けずにサブルーチンを呼出すと、引數竝びがプロトタイプ宣言を合はなければコンパイル・エラーにして呉れるが、アンパサンドを附けて呼出すとエラーに成らなかつた。
此は自分が意圖しない處理を行ふ可能性が在ると云ふ事だから、此は良くないかも。
亦括弧が無い場合は、
% cat test1.pl
#!/usr/bin/perl
use strict;
use warnings;
sub hello($$) {
my (@args) = @_;
print join(':', @args),"\n";
}
&hello 'a';
&hello 'b', 'c';
&hello 'd', 'e', 'f';
hello 'g';
hello 'h', 'i';
hello 'j', 'k', 'l';
% perl test1.pl
String found where operator expected at test1.pl line 10, near "&hello 'a'"
(Missing operator before 'a'?)
String found where operator expected at test1.pl line 11, near "&hello 'b'"
(Missing operator before 'b'?)
String found where operator expected at test1.pl line 12, near "&hello 'd'"
(Missing operator before 'd'?)
syntax error at test1.pl line 10, near "&hello 'a'"
syntax error at test1.pl line 11, near "&hello 'b'"
syntax error at test1.pl line 12, near "&hello 'd'"
Not enough arguments for main::hello at test1.pl line 14, near "'g';"
Too many arguments for main::hello at test1.pl line 16, near "'l';"
Execution of test1.pl aborted due to compilation errors.
アンパサンドを附けて呼出すと、引數竝びがプロトタイプ宣言と合つてゐても括弧が無い場合はコンパイル・エラーに成つた。
知らなかつた。以後、一部の例外を除いてアンパサンドを附けてサブルーチンを呼出すのは止めにする。
此乃日記を今の職場の人に見られたら、莫迦にされて相手にされなくなる事受合ひだな。