Perl Scalars
A scalar is a simple data unit.
A scalar can be an integer, floating-point number, character, string, paragraph, or an entire web page.
The following example demonstrates the simple application of scalars:
Example
Execute the above program, the output result is:
Age = 20 Name = Example Salary = 130.5
Numeric Scalars
A scalar is usually a number or a string. The following example demonstrates the use of different types of numeric scalars:
Example
Execute the above program, the output result is:
integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = 255
String Scalars
The following example demonstrates the use of different types of string scalars. Note the difference between single quotes and double quotes:
Example
Execute the above program, the output result is:
var = 字符串标量 - Example! quote = 我在单引号内 - $var double = 我在双引号内 - 字符串标量 - Example! escape = 转义字符使用 - Hello, World!
Scalar Operations
The following example demonstrates simple scalar operations:
Example
Execute the above program, the output result is:
str = helloworld num = 15 mix = helloworld15
Multiline Strings
We can use single quotes to output multiline strings, as shown below:
Example
Execute the above program, the output result is:
Rookie Tutorial
— What we learn is not only technology, but also dreams!
You can also use the "here" document syntax to output multiple lines:
Example
Execute the above program, the output result is:
Rookie Tutorial
— What we learn is not only technology, but also dreams!
Special Characters
Below we will demonstrate the use of special characters in Perl, such as __FILE__, __LINE__, and __PACKAGE__, which respectively represent the filename, line number, and package name of the currently executing script.
Note: __is two underscores,__FILE__with two underscores on both front and back.
These special characters are separate tokens and cannot be written inside strings, for example:
Example
Execute the above program, the output result is:
文件名 test.pl 行号 4 包名 main __FILE__ __LINE__ __PACKAGE__
v-Strings
A string starting with v, followed by one or more integers separated by dots, is treated as a string literal.
When you want to directly declare the numeric value for each character, v-strings provide a clearer way to construct such strings, unlike "\x{1}\x{14}\x{12c}\x{fa0}" which is not easy to understand. We can look at the following example:
Example
Execute the above program, the output result is:
Wide character in print at test.pl line 7. smile = ☺ foo = foo martin = MartinOther Extensions