在连接(.)中使用未初始化的值$last_name {“dino”}或在./learning.pl第18行使用字符串.
use warnings;use strict;my %last_name = ( fred => 'flintston',dino => undef,barney => 'rubble',betty => 'rubble',);my $key;my $value;if(%last_name) { foreach $key (sort keys %last_name) { print("$key => $last_name{$key}\n"); }}
但是当我将哈希线更改为:
my %last_name = ( fred => 'flintston',dino => undef => barney => 'rubble',);
它工作正常,并返回值为undef.我所做的只是替换使用’=>’分隔键/值的逗号运算符 *** 作符.
如果两个运算符应该是相同的,为什么后面的工作而不是前者?
解决方法 它们并不完全相同. “胖逗号”还做了一件事:当它是一个单词时,它会引用左侧 *** 作数.因此,undef,1
相当于
undef(),1
而
undef => 1
相当于
'undef',1
见perlop:
总结The => operator is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters,digits and underscores.
以上是内存溢出为你收集整理的Perl – Hash和=>运算符全部内容,希望文章能够帮你解决Perl – Hash和=>运算符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)