Getting started...login
Getting started with Perl
author:php.cn  update time:2022-04-14 16:05:12

Perl scalar


A scalar is a simple unit of data.

The scalar can be an integer, floating point number, character, string, paragraph or a complete web page.

The following example demonstrates the simple application of scalars:

#!/usr/bin/perl

$age = 20;             # 整数赋值
$name = "php";   # 字符串
$salary = 130.50;     # 浮点数

print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";

Execute the above program, the output result is:

Age = 20
Name = php
Salary = 130.5

Numeric scalar

Scalar usually Is a number or string. The following examples demonstrate the use of different types of numeric scalars:

#!/usr/bin/perl

$integer = 200;
$negative = -300;
$floating = 200.340;
$bigfloat = -1.2E-23;

# 八进制 377 , 十进制为 255 
$octal = 0377;

# 十六进制 FF, 十进制为 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 scalar

The following examples demonstrate the use of different types of string scalars. Pay attention to the difference between the use of single quotes and double quotes:

#!/usr/bin/perl

$var = "字符串标量 - php中文网!";
$quote = '我在单引号内 - $var';
$double = "我在双引号内 - $var";

$escape = "转义字符使用 -\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 = 字符串标量 - php中文网!
quote = 我在单引号内 - $var
double = 我在双引号内 - 字符串标量 - php中文网!
escape = 转义字符使用 -	Hello, World!

Scalar operation

The following example demonstrates the simple operation of scalar:

#!/usr/bin/perl

$str = "hello" . "world";       # 字符串连接
$num = 5 + 10;                  # 两数相加
$mul = 4 * 5;                   # 两数相乘
$mix = $str . $num;             # 连接字符串和数字

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

Multi-line string

We can use single quotes to output multi-line strings, as shown below:

#!/usr/bin/perl

$string = '
php中文网
	——  php中文网
';

print "$string\n";

Execute the above program, the output result is:

php中文网
	——  php中文网

You can also use "here" document Syntax format to output multiple lines:

#!/usr/bin/perl

print <<EOF;
php中文网
	——  php中文网
EOF

Execute the above program, the output result is:

php中文网
	——  php中文网

Special characters

Below we will demonstrate the application of special characters in Perl , such as __FILE__, __LINE__, and __PACKAGE__ respectively represent the file name, line number, and package name of the currently executing script.

These special characters are separate markers and cannot be written in a string. For example:

#!/usr/bin/perl

print "文件名 ". __FILE__ . "\n";
print "行号 " . __LINE__ ."\n";
print "包名 " . __PACKAGE__ ."\n";

# 无法解析
print "__FILE__ __LINE__ __PACKAGE__\n";

Execute the above program, the output result is:

文件名 test.pl
行号 4
包名 main
__FILE__ __LINE__ __PACKAGE__

v characters String

A string starting with v followed by one or more integers separated by periods will be treated as a string text.

When you want to directly declare the numeric value for each character, v-string provides a clearer way to construct such strings, unlike "\x{1}\x {14}\x{12c}\x{fa0}" This is not easy to understand. Can we look at the following 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 and the output result is:

Wide character in print at test.pl line 7.
smile = ☺
foo = foo
martin = Martin

php.cn