Perl日記

日々の知ったことのメモなどです。Perlは最近やってないです。

my()のプロトタイプ

my()はかっこつけたら何個でも変数を同時に局所化できるけれど、かっこなしだと一個だけしか局所化できない。

use strict;
use warnings;
my (@aaa, @bbb, %aaa, %bbb, $aaa, $bbb);
my @ddd, @eee, %ddd, %eee, $ddd, $eee;
Parentheses missing around "my" list at - line 4.
Global symbol "@eee" requires explicit package name at - line 4.
Global symbol "%ddd" requires explicit package name at - line 4.
Global symbol "%eee" requires explicit package name at - line 4.
Global symbol "$ddd" requires explicit package name at - line 4.
Global symbol "$eee" requires explicit package name at - line 4.
Execution of - aborted due to compilation errors.

だから、左辺値代入で以下のようにできる。

my $outer;
{
  (my $inner, $outer) = size_of_field('rikujyo');
}


三項演算子で選んだ方を局所化もできる。

use strict;
use warnings;
my (1 == 1 ? $aaa : $bbb) = 123;
print "$aaa\n";
print "$bbb\n";
123
Use of uninitialized value $bbb in concatenation (.) or string at - line 5.

perldoc -f my