如何在Perl中使用三元语法执行多个命令?

时间:2014-05-30 03:46:31

标签: perl if-statement

我该如何做以下事情:

(condition) ? (do this) (do this too) : (just do this then)

3 个答案:

答案 0 :(得分:8)

使用do执行此操作。

condition ? do { this(); this_too(); } : the_other_thing()

答案 1 :(得分:2)

你想:

( (do this), (do this too) )

答案 2 :(得分:-3)

执行以下操作的示例程序:

#!/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");