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

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.

主站蜘蛛池模板: 共和县| 从化市| 三都| 开远市| 奎屯市| 道孚县| 新民市| 镶黄旗| 镇宁| 宜章县| 宣城市| 满城县| 曲周县| 台江县| 栾川县| 汽车| 宁远县| 新营市| 钦州市| 绥滨县| 华宁县| 沙雅县| 卢湾区| 龙门县| 平泉县| 乌兰浩特市| 保德县| 永福县| 石屏县| 林芝县| 玛沁县| 永登县| 福海县| 怀集县| 揭阳市| 延川县| 吉首市| 永修县| 梓潼县| 荔波县| 吉水县|