A scalar is a simple unit of data.
A scalar can be an integer, a floating point number, a character, a string, a paragraph, or a complete web page.
The following example demonstrates a simple use of scalars:
Executing the above program, the output result is:
Age = 20Name = CoderctoSalary = 130.5
A scalar is usually a number or a string. The following examples demonstrate the use of different types of numeric scalars:
Executing the above program, the output result is:
integer = 200negative = -300floating = 200.34bigfloat = -1.2e-23octal = 255hexa = 255
The following examples demonstrate the use of different types of string scalars. Pay attention to the difference between single quotes and double quotes:
Executing the above program, the output result is:
var = String scalar - Programmer's Tutorial! quote = I'm in single quotes - $vardouble = I'm in double quotes - String scalar - Programmer's Tutorial! escape = Use escape characters - Hello, World!
The following example demonstrates simple operations on scalars:
Executing the above program, the output result is:
str = helloworldnum = 15mix = helloworld15
We can use single quotes to output multi-line strings as follows:
Executing the above program, the output result is:
Tutorial for coders - learn not only technology, but also dreams!
You can also use the "here" document syntax to output multiple lines:
Executing the above program, the output result is:
Tutorial for coders - learn not only technology, but also dreams!
Below we will demonstrate the application of special characters in Perl, such as __FILE__, __LINE__, and __PACKAGE__, which respectively represent the file name, line number, and package name of the currently executing script.
Note : __ is two underscores, and there are two underscores before and after __FILE__ .
These special characters are separate tokens and cannot be written in a string, for example:
Executing the above program, the output result is:
File name test.pl line number 4 package name main__FILE__ __LINE__ __PACKAGE__
A string starting with v followed by one or more integers separated by periods is treated as a string literal.
When you want to directly declare the numeric value for each character, v-string provides a cleaner way to construct such strings, unlike "x{1}x{14}x{ 12c}x{fa0}" is not easy to understand. We can look at the following examples:
Executing the above program, the output result is:
Wide character in print at test.pl line 7.smile = ☺foo = foomartin = Martin