- 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.
推薦閱讀
- JavaScript全程指南
- R語言經典實例(原書第2版)
- Java系統分析與架構設計
- Python編程:從入門到實踐
- PhoneGap:Beginner's Guide(Third Edition)
- 深度學習:Java語言實現
- Mastering Unity 2D Game Development(Second Edition)
- 軟件測試教程
- Web前端應用開發技術
- 編程可以很簡單
- Java Web從入門到精通(第3版)
- 從零開始學UI:概念解析、實戰提高、突破規則
- 生成藝術:Processing視覺創意入門
- Elasticsearch實戰(第2版)
- Learning Java by Building Android Games