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

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.

主站蜘蛛池模板: 常山县| 隆化县| 泸州市| 长沙市| 易门县| 手机| 池州市| 布尔津县| 陆良县| 盐池县| 于都县| 竹溪县| 宿迁市| 淮滨县| 临沭县| 武定县| 临漳县| 湘乡市| 祁东县| 晋江市| 河北区| 芮城县| 长子县| 茌平县| 高州市| 洪泽县| 盐山县| 五原县| 西宁市| 邢台市| 东源县| 安多县| 德安县| 阿坝| 开原市| 大冶市| 青河县| 福清市| 灵石县| 略阳县| 平舆县|