Perl 简明教程
Perl - Scalars
标量是单个数据单元。该数据可以是整数、浮点数、字符、字符串、段落或整个网页。
A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page.
下面是使用标量变量的一个简单示例−
Here is a simple example of using scalar variables −
#!/usr/bin/perl
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
这会产生以下结果 −
This will produce the following result −
Age = 25
Name = John Paul
Salary = 1445.5
Numeric Scalars
标量最常见的是数字或字符串。以下示例演示各种类型的数字标量的用途 −
A scalar is most often either a number or a string. Following example demonstrates the usage of various types of numeric scalars −
#!/usr/bin/perl
$integer = 200;
$negative = -300;
$floating = 200.340;
$bigfloat = -1.2E-23;
# 377 octal, same as 255 decimal
$octal = 0377;
# FF hex, also 255 decimal
$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";
这会产生以下结果 −
This will produce the following result −
integer = 200
negative = -300
floating = 200.34
bigfloat = -1.2e-23
octal = 255
hexa = 255
String Scalars
以下示例演示了各种类型字符串标量的用法。请注意单引号字符串和双引号字符串之间的区别 −
Following example demonstrates the usage of various types of string scalars. Notice the difference between single quoted strings and double quoted strings −
#!/usr/bin/perl
$var = "This is string scalar!";
$quote = 'I m inside single quote - $var';
$double = "This is inside single quote - $var";
$escape = "This example of escape -\tHello, World!";
print "var = $var\n";
print "quote = $quote\n";
print "double = $double\n";
print "escape = $escape\n";
这会产生以下结果 −
This will produce the following result −
var = This is string scalar!
quote = I m inside single quote - $var
double = This is inside single quote - This is string scalar!
escape = This example of escape - Hello, World
Scalar Operations
您将在单独的章节中看到 Perl 中可用的各种运算符的详细信息,但我们在这里列出一些数字和字符串操作。
You will see a detail of various operators available in Perl in a separate chapter, but here we are going to list down few numeric and string operations.
#!/usr/bin/perl
$str = "hello" . "world"; # Concatenates strings.
$num = 5 + 10; # adds two numbers.
$mul = 4 * 5; # multiplies two numbers.
$mix = $str . $num; # concatenates string and number.
print "str = $str\n";
print "num = $num\n";
print "mul = $mul\n";
print "mix = $mix\n";
这会产生以下结果 −
This will produce the following result −
str = helloworld
num = 15
mul = 20
mix = helloworld15
Multiline Strings
如果您想将多行字符串引入程序中,可以使用标准单引号,如下所示 −
If you want to introduce multiline strings into your programs, you can use the standard single quotes as below −
#!/usr/bin/perl
$string = 'This is
a multiline
string';
print "$string\n";
这会产生以下结果 −
This will produce the following result −
This is
a multiline
string
您还可以使用“here”文档语法来存储或打印多行,如下所示 −
You can use "here" document syntax as well to store or print multilines as below −
#!/usr/bin/perl
print <<EOF;
This is
a multiline
string
EOF
这也会产生相同的结果 −
This will also produce the same result −
This is
a multiline
string
V-Strings
形式为 v1.20.300.4000 的文本被解析为由指定序数的字符组成的字符串。此形式称为 v 字符串。
A literal of the form v1.20.300.4000 is parsed as a string composed of characters with the specified ordinals. This form is known as v-strings.
与使用不太可读的插形式“\x{1}\x{14}\x{12c}\x{fa0}”相比,v 字符串提供了一种替代的且更具可读性的方式来构造字符串。
A v-string provides an alternative and more readable way to construct strings, rather than use the somewhat less readable interpolation form "\x{1}\x{14}\x{12c}\x{fa0}".
它们是从 v 开头的任何文本,后跟一个或多个用点分隔的元素。例如 −
They are any literal that begins with a v and is followed by one or more dot-separated elements. For 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";
这也会产生相同的结果 −
This will also produce the same result −
smile = ☺
foo = foo
martin = Martin
Wide character in print at main.pl line 7.
Special Literals
到目前为止,您一定对字符串标量及其连接和插值操作有所了解。因此,让我告诉你三个特殊的文本 FILE 、 LINE 和 PACKAGE 表示程序中当前的文件名、行号和软件包名称。
So far you must have a feeling about string scalars and its concatenation and interpolation opration. So let me tell you about three special literals FILE, LINE, and PACKAGE represent the current filename, line number, and package name at that point in your program.
它们只能用作单独的标记,并且不会插值到字符串中。查看以下示例 −
They may be used only as separate tokens and will not be interpolated into strings. Check the below example −
#!/usr/bin/perl
print "File name ". __FILE__ . "\n";
print "Line Number " . __LINE__ ."\n";
print "Package " . __PACKAGE__ ."\n";
# they can not be interpolated
print "__FILE__ __LINE__ __PACKAGE__\n";
这会产生以下结果 −
This will produce the following result −
File name hello.pl
Line Number 4
Package main
__FILE__ __LINE__ __PACKAGE__