パスワードを忘れた? アカウント作成
249992 journal

tanigの日記: perlの挙動

日記 by tanig

perl の動作で解釈出来ないものがある。

--test.pl
#!/usr/local/bin/perl --
        eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
                if 0; #$running_under_some_shell
use warnings;
my @s = ("a", "b");
foreach my $a ("0", "1") {
    &do($a, $_) foreach (@s);
}

sub do {
    my $a = shift;
    my $b = shift;
    print "$a, $b\n";
    open(my $r, "<", "test.pl");
    while(<$r>) {
    }
    close($r);
}
__END__

の結果が
0, a
0, b
Use of uninitialized value $b in concatenation (.) or string at ./test.pl line 13.
1,
Use of uninitialized value $b in concatenation (.) or string at ./test.pl line 13.
1,
となる。
while() で $_ に値が読込まれて、do 呼び出し元の @s が書き換えられているようだが、

# 10/10/12 追記
 サンプルコード中の <, > がタグと解釈されていたので、&lt;, &gt; に書き換え

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

吾輩はリファレンスである。名前はまだ無い -- perlの中の人

読み込み中...