Latest web development tutorials

Perl scalar

Scalar is a simple data elements.

Scalar can be an integer, floating point numbers, characters, strings, paragraphs or a complete page.

The following example demonstrates a simple scalar application:

#!/usr/bin/perl

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

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

The above program, the output is:

Age = 20
Name = w3big
Salary = 130.5

Digital scalar

Scalar usually a number or string, the following example demonstrates the use of different digital scalar types:

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

The above program, the output is:

integer = 200
negative = -300
floating = 200.34
bigfloat = -1.2e-23
octal = 255
hexa = 255

String-scalar

The following example demonstrates string scalar using different types of and attention to the difference between the single and double quotes:

#!/usr/bin/perl

$var = "字符串标量 - 本教程!";
$quote = '我在单引号内 - $var';
$double = "我在双引号内 - $var";

$escape = "转义字符使用 -\tHello, World!";

print "var = $var\n";
print "quote = $quote\n";
print "double = $double\n";
print "escape = $escape\n";

The above program, the output is:

var = 字符串标量 - 本教程!
quote = 我在单引号内 - $var
double = 我在双引号内 - 字符串标量 - 本教程!
escape = 转义字符使用 -	Hello, World!

Scalar operations

The following example demonstrates a simple scalar operation:

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

The above program, the output is:

str = helloworld
num = 15
mix = helloworld15

Multi-line strings

We can use single quotes to export multi-line string, as follows:

#!/usr/bin/perl

$string = '
本教程
	—— 学的不仅是技术,更是梦想!
';

print "$string\n";

The above program, the output is:

本教程
	—— 学的不仅是技术,更是梦想!

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

#!/usr/bin/perl

print <<EOF;
本教程
	—— 学的不仅是技术,更是梦想!
EOF

The above program, the output is:

本教程
	—— 学的不仅是技术,更是梦想!

Special characters

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

These special characters are separate tag can not be written in a string, for example:

#!/usr/bin/perl

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

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

The above program, the output is:

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

v String

V a beginning, followed by one or more integers separated by periods, and will be treated as a string literal.

When you want to directly declare its numeric value for each character, v- string provides a clearer method for constructing such a string, rather than "\ x {1} \ x {14} \ x { 12c} \ x {fa0} "this is not easy to understand, what I can see the following examples:

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

The above program, the output is:

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