只有在参数为false时才能定义变量吗?
我有类似的东西:
my $object = Class::create($args);
for my $i (@input) {
if ($test) {
# print some info here
next;
}
#use of $object here
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做:
my $object = Class::create($args) unless ($test);
Run Code Online (Sandbox Code Playgroud)
这是一个正确的初始化方法吗?
这种语法...... 充满了危险.
my $object = $test ? undef : Class::create($args);
my $object;
$object = Class::create($args) unless $test;
Run Code Online (Sandbox Code Playgroud)
是更安全的替代品.