官术网_书友最值得收藏!

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 266字
  • 2021-07-03 00:05:41

Sigils

One of the most difficult things for the newcomers to Perl are sigils. A sigil is a character in front of the name of variables in Perl that denotes the structural type of a variable. For example, $ for scalar values, @ for arrays, and % for hashes.

The problem arises when you access an element of an array or hash. Let's consider the following lines of code as an example in Perl 5 of an array with the first few Fibonacci numbers:

my @fibonacci = (0, 1, 1, 2, 3, 5, 8, 13);
print $fibonacci[4];

First, a @fibonacci array is created. The name of the variable includes the @ character as a sigil. In the second line, we access one of the elements of that array and use another sigil, $, this time. This is because a single element of an array is a scalar, and scalars use $ as a sigil. For those who learn Perl, this small change in sigils is a big problem in understanding the basics of the language.

In Perl 6, sigils are unified and are part of the variable names. You do not change it independently, whether you access an element of an array or an array as a whole. The preceding example will look like this in Perl 6:

my @fibonacci = (0, 1, 1, 2, 3, 5, 8, 13);
print @fibonacci[4];

In both lines, the same sigil is used for the @fibonacci array and for its @fibonacci[4] element. This approach is much more consistent and easier for a beginner.

主站蜘蛛池模板: 启东市| 龙泉市| 山阴县| 清水河县| 尖扎县| 连平县| 富蕴县| 邓州市| 长武县| 桐梓县| 晋宁县| 林口县| 马鞍山市| 巴林左旗| 盖州市| 罗定市| 思南县| 通榆县| 佛冈县| 蒲江县| 同仁县| 渭源县| 津南区| 原平市| 阜新市| 衡阳县| 堆龙德庆县| 汽车| 海城市| 井陉县| 浪卡子县| 泾阳县| 五莲县| 屏南县| 晋中市| 永寿县| 新安县| 壶关县| 大城县| 灵丘县| 东山县|