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

  • Perl 6 Deep Dive
  • Andrew Shitov
  • 207字
  • 2021-07-03 00:05:48

Scalars

A scalar is a container that can keep a single value, such as an integer, a string, or an object.

Scalar variables use the $ sigil. We have seen a few examples in the previous sections, and here are some more. Notice that the same scalar variable, if it is not explicitly declared with a data type, can host a value of different types at different moments:

my $x = 42;
say $x;
my $y = $x * 2;
say $y;

$x = 'Hello, World!';
say $x;

(Of course, it is better not to change the type of the data during the program flow.)

Inside the strings in double quotes, scalar variables are interpolated and replaced by their current values. In the following program, the process of calculating an equation is printed as a string:

my $a = 3;
my $b = 4;
my $c = sqrt($a * $a + $b * $b);

say "If the legs of a right triangle are $a and $b, ";
say "then the hypotenuse is $c.";

This code prints the following output:

If the legs of a right triangle are 3 and 4, 
then the hypotenuse is 5.

Now, let's move on to the next type of variables—arrays.

主站蜘蛛池模板: 连城县| 渑池县| 田东县| 岐山县| 安国市| 普陀区| 宜良县| 鄂托克旗| 梧州市| 龙里县| 铁岭市| 宜丰县| 德阳市| 犍为县| 鹿邑县| 九龙坡区| 什邡市| 商城县| 商城县| 常熟市| 吉林市| 白银市| 玛纳斯县| 白银市| 封丘县| 闸北区| 屏东县| 民乐县| 巴里| 和田县| 长沙县| 静安区| 习水县| 繁峙县| 舞钢市| 庄浪县| 保亭| 启东市| 来安县| 丰都县| 铜川市|