• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Perl]uselessuseofaconstantinavoidcontext

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
      Do you understand context? It freaked me out once or fifty times too. =) When perl is compiling your code, it looks at structures like these and decides on what "context" codehere() is in:
codehere(); #void context 
$c=codehere(); #scalar context 
($l)=codehere(); #list context 
@a=codehere(); #list context 
%h=codehere(); #list context 
print codehere(); #list context 
foreach (codehere()) { #list context 
if ( 1 <= codehere() ) { #scalar context

 

When you do something along the lines of this: $a=( codehere(),codehere() ); 

then the $a= forces the right side into scalar context since perl can see you don't want a list so in scalar context, the right side isn't treated as a list but as a statement group. Thus, "," is an operator that forces void context on it's left-hand-side and passes on the whatever context it is in to it's right-hand-side. Since you strung together a series of values like this: $a = ( 2, 3, 4, 5, 6 ); the first four numbers are in void context and only the last item in the series is in scalar context. Thus your statement could be rewritten: 2; 3; 4; 5; $a = 6;

And, bam!, from that you can see there are 4 constants in void context. Run either of those with perl -we on the command line and enjoy seeing the 4 warnings pop-up.

BTW, $a = ( 1, 5 ); pulls no error. =) It seems that perl and in fact Perl treat 1; special.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
LINUX下设置定时运行PERL脚本发布时间:2022-07-22
下一篇:
Perl判断操作系统类型发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap