- 论坛徽章:
- 0
|
本帖最后由 picbhan 于 2013-05-17 19:23 编辑
- %hash = map { "\L$_" => 1 } @array # perl guesses EXPR. wrong
- %hash = map { +"\L$_" => 1 } @array # perl guesses BLOCK. right
- %hash = map { ("\L$_" => 1) } @array # this also works
- %hash = map { lc($_) => 1 } @array # as does this.
- %hash = map +( lc($_) => 1 ), @array # this is EXPR and works!
- %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
- or to force an anon hash constructor use "+{":
- @hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs comma at end
复制代码 我的理解是这样的。
map有两种用法:
map BLOCK LIST
map EXPR,LIST
{}里简单的表达式如map {0,0} () perl会倾向于理解为expr,而把{}当做普通的作用域,因此需要‘,’分隔至少两个参数(如map {0,0}, ()),而{+}或者{}里复杂的表达式会理解为BLOCK,但是+{}会被强制当做EXPR理解。只是这里简单和复杂的表达式不是很好定义,可以这样定义,即任何语法操作(如简单变量{0},{@a},一元的+,强制返回列表的(),函数操作lc,以及‘;’等等)可以当做是复杂的表达式,其余可以当做是简单表达式(如{0,0}等)。
个人理解,勿喷,勿拍砖! |
|