Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Scalars

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)

print

"

Age =

$age

\n

"

;

print

"

Name =

$name

\n

"

;

print

"

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

;

print

"

integer =

$integer

\n

"

;

print

"

negative =

$negative

\n

"

;

print

"

floating =

$floating

\n

"

;

print

"

bigfloat =

$bigfloat

\n

"

;

print

"

octal =

$octal

\n

"

;

print

"

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!

"

;

print

"

var =

$var

\n

"

;

print

"

quote =

$quote

\n

"

;

print

"

double =

$double

\n

"

;

print

"

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

print

"

str =

$str

\n

"

;

print

"

num =

$num

\n

"

;

print

"

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!

'

;

print

"

$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

print

<<

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

print

"

File Name

"

.

FILE

.

"

\n

"

;

print

"

line number

"

.

LINE

.

"

\n

"

;

print

"

Package name

"

.

PACKAGE

.

"

\n

"

;

Cannot parse

print

"

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

;

print

"

smile =

$smile

\n

"

;

print

"

foo =

$foo

\n

"

;

print

"

martin =

$martin

\n

"

;

Running the above program, the output is:



Wide character in print at test.pl line 7.

smile = ☺

foo = foo

martin = Martin