Like Share Discussion Bookmark Smile

J.J. Huang   2021-03-08   Perl   瀏覽次數:次   DMCA.com Protection Status

Perl - 第五章 | Perl 標量

標量是資料的單個單位。該資料可能是整數,浮點數,字符,字符串,段落或整個網頁。

這是一個使用標量變量的簡單範例:

1
2
3
4
5
6
7
8
9
#!/usr/bin/perl

$age = 25; # An integer assignment
$name = "J.J."; # A string
$salary = 1445.50; # A floating point

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

這將產生以下結果:

1
2
3
Age = 25
Name = J.J.
Salary = 1445.5

數字標量

標量通常是數字或字符串。以下範例演示了各種類型的數字標量的用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/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";

這將產生以下結果:

1
2
3
4
5
6
integer = 200
negative = -300
floating = 200.34
bigfloat = -1.2e-23
octal = 255
hexa = 255

字符串標量

以下範例演示了各種類型的字符串標量的用法。注意單引號字符串和雙引號字符串之間的區別:

1
2
3
4
5
6
7
8
9
10
11
12
#!/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";

這將產生以下結果:

1
2
3
4
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

標量運算

你將在單獨的章節中看到 Perl 中可用的各種運算符的詳細訊息,但是在這裡,我們將列出一些數字和字符串運算。

1
2
3
4
5
6
7
8
9
10
11
#!/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";

這將產生以下結果:

1
2
3
4
str = helloworld
num = 15
mul = 20
mix = helloworld15

多行字符串

如果要在程序中引入多行字符串,則可以使用以下標准單引號:

1
2
3
4
5
6
7
#!/usr/bin/perl

$string = 'This is
a multiline
string';

print "$string\n";

這將產生以下結果:

1
2
3
This is
a multiline
string

你還可以使用”Here”文件語法來存儲或打印多行,如下所示:

1
2
3
4
5
6
7
#!/usr/bin/perl

print <<EOF;
This is
a multiline
string
EOF

這將產生以下結果:

1
2
3
This is
a multiline
string

v 字符串

一個以 v 開頭,後面跟著一個或多個用句點分隔的整數,會被當作一個字串文本。

當你想為每個字符直接聲明其數字值時,v-字串提供了一種更清晰的構造這類字串的方法,而不像”\x{1}\x{14}\x{ 12c}\x{fa0}” 這種不易於理解,我麼可以看下面的範例:

1
2
3
4
5
6
7
8
9
#!/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";

這將產生以下結果:

1
2
3
4
smile = ☺
foo = foo
martin = Martin
Wide character in print at main.pl line 7.

特殊文字

到目前為止,你必須對字符串標量及其連接和插值運算有一定的了解。因此,讓我告訴你三個特殊的文字__FILE__,__LINE__和__PACKAGE__代表程序中該點的當前文件名,行號和程序包名稱。

它們只能用作單獨的標記,而不會內插到字符串中。檢查以下範例:

1
2
3
4
5
6
7
8
#!/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";

這將產生以下結果:

1
2
3
4
File name hello.pl
Line Number 4
Package main
__FILE__ __LINE__ __PACKAGE__

註:以上參考了
Tutorialspoint, Perl - Scalars