Perl Scalars
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 even a complete web page.
The following example demonstrates a simple application of scalars:
Example
#!/usr/bin/perl
$age
=
20
;
Integer assignment
$name
=
"
Runoob
"
;
String
$salary
=
130.50
;
Float (floating point number)
"
Age =
$age
\n
"
;
"
Name =
$name
\n
"
;
"
Salary =
$salary
\n
"
;
Running the above program, the output is:
Age = 20
Name = Runoob
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
#!/usr/bin/perl
$integer
=
200
;
$negative
= -
300
;
$floating
=
200.340
;
$bigfloat
= -
1.2
E
23
;
Octal 377, which is 255 in decimal
$octal
=
0377
;
Hexadecimal FF, which is 255 in decimal
$hexa
=
0
xff
;
"
integer =
$integer
\n
"
;
"
negative =
$negative
\n
"
;
"
floating =
$floating
\n
"
;
"
bigfloat =
$bigfloat
\n
"
;
"
octal =
$octal
\n
"
;
"
hexa =
$hexa
\n
"
;
Running the above program, the output 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
#!/usr/bin/perl
$var
=
"
String scalar - Runoob Tutorial!
"
;
$quote
=
'
I am inside single quotes - $var
'
;
$double
=
"
I am inside double quotes -
$var
"
;
$escape
=
"
Escape character usage -
\t
Hello, World!
"
;
"
var =
$var
\n
"
;
"
quote =
$quote
\n
"
;
"
double =
$double
\n
"
;
"
escape =
$escape
\n
"
;
Running the above program, the output is:
var = 字符串标量 - Runoob Tutorial!
quote = 我在单引号内 - $var
double = 我在双引号内 - 字符串标量 - Runoob Tutorial!
escape = 转义字符使用 - Hello, World!
Scalar operations
The following example demonstrates simple scalar operations:
Example
#!/usr/bin/perl
$str
=
"
hello
"
.
"
world
"
;
String concatenation
$num
=
5
10
;
add two numbers
$mul
=
4
5
;
multiply two numbers
$mix
=
$str
.
$num
;
Concatenating a string and a number
"
str =
$str
\n
"
;
"
num =
$num
\n
"
;
"
mix =
$mix
\n
"
;
Running the above program, the output is:
str = helloworld
num = 15
mix = helloworld15
Multiline Strings
We can use single quotes to output a multi-line string, as shown below:
Example
#!/usr/bin/perl
$string
=
'
Runoob Tutorial —— what you learn is not only technology, but also dreams!
'
;
"
$string
\n
"
;
Running the above program, the output is:
Runoob Tutorial
—— 学的不仅是技术,更是梦想!
You can also use the "here" document syntax to output multiple lines:
Example
#!/usr/bin/perl
<<
EOF
; Runoob Tutorial —— what you learn is not only technology, but also dreams!
EOF
Running the above program, the output is:
Runoob Tutorial
—— 学的不仅是技术,更是梦想!
Special characters
Below we will demonstrate the use of special characters in Perl, such as FILE, LINE, and LinkedHashMap, among others. PACKAGE which respectively represent the filename, line number, and package name of the currently executing script.
Note: __ is two underscores,FILE with two underscores before and after.
These special characters are standalone tokens and cannot be written inside strings, for example:
Example
#!/usr/bin/perl
"
File Name
"
.
FILE
.
"
\n
"
;
"
line number
"
.
LINE
.
"
\n
"
;
"
Package name
"
.
PACKAGE
.
"
\n
"
;
Cannot parse
"
FILE LINE PACKAGE
\n
"
;
Running the above program, the output is:
文件名 test.pl
行号 4
包名 main
__FILE__ __LINE__ __PACKAGE__
v-strings
A string that starts 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 of each character, v-strings provide a clearer way to construct such strings, unlike the hard-to-understand "\x{1}\x{14}\x{12c}\x{fa0}". Let's look at the following example:
Example
#!/usr/bin/perl
$smile
=
v9786
;
$foo
=
v102
.111
.111
;
$martin
=
v77
.97
.114
.116
.105
.110
;
"
smile =
$smile
\n
"
;
"
foo =
$foo
\n
"
;
"
martin =
$martin
\n
"
;
Running the above program, the output is:
Wide character in print at test.pl line 7.
smile = ☺
foo = foo
martin = Martin