Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何将反向if语句连接到字符串?_String_Perl_If Statement_Concatenation - Fatal编程技术网

String 如何将反向if语句连接到字符串?

String 如何将反向if语句连接到字符串?,string,perl,if-statement,concatenation,String,Perl,If Statement,Concatenation,为什么最后一行不编译?它不编译,因为if的形式是语句修饰符;它只能在语句末尾使用,不能在表达式的其他位置使用 你可以做: use DateTime::Format::Natural; $parser = new DateTime::Format::Natural; $dt = $parser->parse_datetime("1 Test 2010"); print "Date: " . ($dt->dmy('/') if $parser->success); 或: (尽管后

为什么最后一行不编译?

它不编译,因为
if的形式是语句修饰符;它只能在语句末尾使用,不能在表达式的其他位置使用

你可以做:

use DateTime::Format::Natural;
$parser = new DateTime::Format::Natural;
$dt = $parser->parse_datetime("1 Test 2010");
print "Date: " . ($dt->dmy('/') if $parser->success);
或:


(尽管后者将尝试打印$parser->success,如果它为false,那么它将打印“Date:0”。

它不会编译,因为
if的形式是语句修饰符;它只能在语句末尾使用,不能在表达式的其他位置使用

你可以做:

use DateTime::Format::Natural;
$parser = new DateTime::Format::Natural;
$dt = $parser->parse_datetime("1 Test 2010");
print "Date: " . ($dt->dmy('/') if $parser->success);
或:


(尽管后者将尝试打印$parser->success(如果为false),并且在这种情况下将打印“Date:0”。

也许在这里使用三元运算符

print "Date: " . do { $dt->dmy('/') if $parser->success };
或者将代码拆分为两条语句

print 'Date: ' . ($parser->success ? $dt->dmy('/') : '');

也许在这里使用三元运算符

print "Date: " . do { $dt->dmy('/') if $parser->success };
或者将代码拆分为两条语句

print 'Date: ' . ($parser->success ? $dt->dmy('/') : '');

请接受答案以结束问题。请参阅:请接受答案以结束问题。见: