@arr1
このコードからの出力としての最後の要素を期待しています:
#!/usr/bin/perl
my @arr1 = qw(son kon bon won kon don pon won pon don won);
my $innr_element = '';
foreach $innr_element ( @arr1 ) {
## do something
}
print "--->$innr_element<---\n";
しかし、何も得られません (空白の出力)。$innr_element
ブロックスコープの変数 (foreach の内部) として Perl によって内部的に作成されている場合、以下は適切に機能するはずです。
#!/usr/bin/perl
use strict;
my @arr1 = qw(son kon bon won kon don pon won pon don won);
#my $innr_element = '';
foreach $innr_element ( @arr1 ) {
##do something
}
print "--->$innr_element<---\n";
しかし、上記のコードは以下のエラーを返します。
Global symbol "$innr_element" requires explicit package name at test.pl line 5.
Global symbol "$innr_element" requires explicit package name at test.pl line 8.
Execution of test.pl aborted due to compilation errors.
したがって、Perl が内部変数を暗黙的に作成していないことは明らかです。
この文書にも同じことが書かれています。If you declare VAR with my, the scope of the variable will extend throughout the foreach statement, but not beyond it.
これは別の perl マジックですか、それとも何か不足していますか?