双箭头(=>)运算符在Perl中如何工作? [英] How does double arrow (=>) operator work in Perl?

查看:109
本文介绍了双箭头(=>)运算符在Perl中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 => 运算符的哈希用法, like this

I know about the hash use of the => operator, like this

$ cat array.pl
%ages = ('Martin' => 28,
         'Sharon' => 35,
         'Rikke' => 29,);

print "Rikke is $ages{Rikke} years old\n";
$ perl array.pl
Rikke is 29 years old
$



<我认为这只是初始化散列的语法,但是在我如何在Perl中限定一个变量为const / final? => 已被使用像这样

use Readonly;
Readonly my $infilename => "input_56_12.txt";

=>> 是什么意思?是否有更多方式可以使用 =>

What exactly does => mean? Are there more ways in which => can be used?

推荐答案

perl中的 => 运算符与逗号基本相同。唯一的区别是,如果左侧有一个没有引号的单词,它就像引用的单词一样对待。所以你可以写成 Martin => 28 ,它与'Martin',28

The => operator in perl is basically the same as comma. The only difference is that if there's an unquoted word on the left, it's treated like a quoted word. So you could have written Martin => 28 which would be the same as 'Martin', 28.

相同在任何长度的列表中做一个哈希,这就是你的例子。

You can make a hash from any even-length list, which is all you're doing in your example.

您的Readonly示例利用Perl灵活的子程序参数省略括号。它相当于 Readonly(我的$ infilename,input_56_12.txt)。 Readonly是由Readonly模块导出的函数,它具有两个参数:引用和值。如果你想了解它们,Readonly的内部值得另一个问题。

Your Readonly example is taking advantage of Perl's flexibility with subroutine arguments by omitting the parenthesis. It is equivalent to Readonly(my $infilename, "input_56_12.txt"). Readonly is a function exported by the Readonly module which takes two arguments: a reference, and a value. The internals of Readonly are worthy of another question if you want to understand them.

下面是一个以意想不到的方式将它用作逗号的例子:

Here's an example of using it as a comma in an unexpected way:

$ perl -e 'print hello => "world\n"'
helloworld

这篇关于双箭头(=&gt;)运算符在Perl中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆