惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
F
Fortinet All Blogs
B
Blog
GbyAI
GbyAI
P
Proofpoint News Feed
量子位
The Register - Security
The Register - Security
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
B
Blog RSS Feed
WordPress大学
WordPress大学
Recorded Future
Recorded Future
Recent Announcements
Recent Announcements
V
Vulnerabilities – Threatpost
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
雷峰网
雷峰网
Stack Overflow Blog
Stack Overflow Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
O
OpenAI News
月光博客
月光博客
H
Hacker News: Front Page
S
Security Affairs
W
WeLiveSecurity
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Help Net Security
Help Net Security
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
S
Securelist
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
A
About on SuperTechFans

博客园 - 古月春秋(刘云涛)

打车抬表费用图 vi的一点小经验 修正mysqlcc在MySQL 5.0上常报的 Table 'xxx' doesn't exist 错误 Ubuntu 5.10 下 Apache2 SSL 的配置方法 一行shell命令求素数 Boot FreeBSD from Windows boot loader Turtles - So Happy Together 关于IoC、低耦合、配置文件及其本质意义的思考 Perl与数据库 Perl无废话入门指南 W3C XSL Transformations (XSLT) Version 2.0 翻译计划 新版 apache_2.0.54 php-5.0.4 mysql-4.1.12a 组合安装向导(原创) Solaris 下安装Perl的DBD-mysql模块失败的原因以及解决办法 Why MSN Messenger ask me to verify email address all the time? SharpDevelop的AddInTree View 插件 SharpDevelop源码分析 (三、插件系统) SharpDevelop源码分析 (二、主程序+隐藏的初始化) SharpDevelop代码分析 (一、序+基本概念) 亚洲3S节目表
Perl中不寻常的 ?: 运算符 - 古月春秋(刘云涛)
古月春秋(刘云涛) · 2006-04-01 · via 博客园 - 古月春秋(刘云涛)

前几天写一个perl的脚本 在:?运算符上遇到了一个很诡异的问题

$data->{$id}->{'total'?
    
$data->{$id}->{'ratio'= sprintf("%.2f%%", 100 * $data->{$id}->{'succ'/ $data->{$id}->{'total'}) : 
    
$data->{$id}->{'ratio'= 'N/A';

我的本意是 如果 $data->{$id}->{'total'} 未定义则不计算ratio,把ratio赋值为N/A. 这条语句等同于

if ( $data->{$id}->{'total'} ) {
    
$data->{$id}->{'ratio'= sprintf("%.2f%%", 100 * $data->{$id}->{'succ'/ $data->{$id}->{'total'});
else {
    
$data->{$id}->{'ratio'= 'N/A';

可奇怪的是,当无论total是否有定义 ratio的结果居然都是N/A. 可后面if……else……的语句是没有问题的,真的是让我百思不得其解.
跑去查Perl的文档, 其中对于?:的运算符号的解释是
Ternary ``?:'' is the conditional operator, just as in C. It works much like an if-then-else. If the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned.

貌似是return the argument, 于是乎 我突然有了一个想法, 在前后都加上了括号……

$data->{$id}->{'total'?
    ( 
$data->{$id}->{'ratio'= sprintf("%.2f%%", 100 * $data->{$id}->{'succ'/ $data->{$id}->{'total'}) ) : 
    ( 
$data->{$id}->{'ratio'= 'N/A' );

……居然就对了. 既然是return the argument, 我就又换了一种方式:

$data->{$id}->{'ratio'= $data->{$id}->{'total'?
    
sprintf("%.2f%%", 100 * $data->{$id}->{'succ'/ $data->{$id}->{'total'}) : 
    
'N/A';

虽然后面两种方式都可以理解, 那确实是一种正确的做法. 但为什么第一种方式的结果不对呢? 难道return the argument的意思就是不要在那里做赋值运算吗? 

为了测试, 我又写了一个简单的小程序

 1 #!/usr/bin/perl
 2 
 3 use strict;
 4 
 5 my $total=1;
 6 my $rval;
 7 
 8 ############################
 9 $total ? 
10     $rval = $total :
11     $rval = 'N/A';
12 
13 print $rval, "\n";
14 
15 ############################
16 $total ? 
17     ( $rval = $total ) :
18     ( $rval = 'N/A' );
19 
20 print $rval, "\n";
21 
22 ############################
23 $rval = $total ? $total : 'N/A';
24 
25 print $rval, "\n";
26 
27 ############################
28 if ($total) {
29     $rval = $total;
30 else {
31     $rval = 'N/A';
32 }
33 
34 print $rval;

运行的结果显示, 无论第5行给$total赋什么值……包括1, "abc", "true", undef 等,执行的结果第一个print打印出来的都是N/A. 难道 $total? 不等价于 if ($total) 吗?

后来偶然的一次机会在PerlChina上问过此问题,才猛然发现原来C和Perl对于:?和=的优先级定义是不同的。在C中,=的优先级高于:?,而Perl中则正好相反。这直接导致了第一种情况对于语句的解释顺序与C截然不同。