- 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.
推薦閱讀
- Mastering AWS Lambda
- Microsoft Dynamics 365 Extensions Cookbook
- 云原生Spring實戰
- Java性能權威指南(第2版)
- 軟件架構:Python語言實現
- Spring Boot Cookbook
- Java EE 8 Application Development
- 編程與類型系統
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎卷)
- Visualforce Developer’s guide
- Modernizing Legacy Applications in PHP
- Python全棧開發:基礎入門
- 青少年Python趣味編程
- Design Patterns and Best Practices in Java
- 區塊鏈:技術與場景