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

#!/usr/bin/perl $age = 20; # Integer assignment $name = "Example"; # String $salary = 130.50; # Floating-point number print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n";

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

#!/usr/bin/perl $integer = 200; $negative = -300; $floating = 200.340; $bigfloat = -1.2E-23; # Octal 377, decimal 255 $octal = 0377; # Hexadecimal FF, decimal 255 $hexa = 0xff; print "integer = $integer\n"; print "negative = $negative\n"; print "floating = $floating\n"; print "bigfloat = $bigfloat\n"; print "octal = $octal\n"; print "hexa = $hexa\n";

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

#!/usr/bin/perl $var = "String scalar - Example Tutorial!"; $quote = 'I am inside single quotes - $var'; $double = "I am inside double quotes -$var"; $escape = "Escape character usage -\tHello, World!"; print "var = $var\n"; print "quote = $quote\n"; print "double = $double\n"; print "escape = $escape\n";

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

#!/usr/bin/perl $str = "hello" . "world"; # String concatenation $num = 5 + 10; # Add two numbers $mul = 4 * 5; # Multiply two numbers $mix = $str . $num; # Concatenate string and number print "str = $str\n"; print "num = $num\n"; print "mix = $mix\n";

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

#!/usr/bin/perl $string = 'Example Tutorial — What we learn is not only technology, but also dreams!'; print "$string\n";

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

#!/usr/bin/perl print <<EOF; Example Tutorial — What we learn is not only technology, but also dreams!EOF

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

#!/usr/bin/perl print "Filename". __FILE__ . "\n"; print "Line number" . __LINE__ ."\n"; print "Package name" . __PACKAGE__ ."\n"; # Cannot be parsed print "__FILE__ __LINE__ __PACKAGE__\n";

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

#!/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";

Execute the above program, the output result is:

Wide character in print at test.pl line 7.
smile = &#x263a;
foo = foo
martin = Martin
Other Extensions