如何在Perl中使用三元语法执行多个命令?
我如何才能执行以下操作:如何在Perl中使用三元语法执行多个命令?,perl,if-statement,Perl,If Statement,我如何才能执行以下操作: (condition) ? (do this) (do this too) : (just do this then) 执行以下操作的示例程序: #!/usr/bin/perl my $a = 1; $result = ($a > 1) ? (print "\nDo this\n") ? (print "\nDo this also\n") ? "" : (print "\nOr else do this\n"); $a = 6;
(condition) ? (do this) (do this too) : (just do this then)
执行以下操作的示例程序:
#!/usr/bin/perl
my $a = 1;
$result = ($a > 1) ? (print "\nDo this\n") ? (print "\nDo this also\n") ? "" :
(print "\nOr else do this\n");
$a = 6;
$result = ($a > 1) ? (print "\nDo this\n") ? (print "\nDo this also") ? "" :
(print "\nOr else do this\n");
你想要:
( (do this), (do this too) )
使用
Do
执行此操作和此操作
condition ? do { this(); this_too(); } : the_other_thing()
无论如何,我的尝试失败了。不确定哪里出错了…为什么不使用常规的
if
语句流控制?这更简洁,可读性更强。您还没有解释为什么要使用产生值的三元条件运算符。