○ハッシュを初期化する.
undef %hash_name;
○デフォルトのスカラー変数もundefで初期化されている.
# 実はundefで初期化
my $scalar_variable;
○リストでいらないものを省く
$list = (1, 2, 3, 4, 5);
(undef, undef, $a, $b, $c) = @list;
みたいな感じ.
○関数の戻り値をundefにする
sub func{
if(条件A){
return $result1
}
# エラーの時など
else{
return undef;
}
}
$result = func();
if($rv == undef){
print "エラーです\n";
}
else{
print "結果は$resultです\n";
}
みたいな感じ.
配列を返す関数でもundefを返す場合は
配列の一番目の要素にundefが入るので
それを見てエラーハンドリングできる