- 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.
推薦閱讀
- Learning Single:page Web Application Development
- Learning C# by Developing Games with Unity 2020
- 自己動手實現(xiàn)Lua:虛擬機、編譯器和標準庫
- 動手玩轉(zhuǎn)Scratch3.0編程:人工智能科創(chuàng)教育指南
- Python機器學習實戰(zhàn)
- INSTANT Mercurial SCM Essentials How-to
- 大學計算機基礎(chǔ)實驗指導
- .NET 3.5編程
- Building Serverless Web Applications
- R的極客理想:量化投資篇
- Solr權(quán)威指南(下卷)
- Learning Ionic(Second Edition)
- Java Script從入門到精通(第5版)
- 大話C語言
- C語言開發(fā)寶典