当前位置:首页 » Perl » Perl标量

Perl标量变量

Perl标量变量,实例,使用例子在线教程:标量变量简单变量只包含一个元素 - 一个字符串或数字。

标量变量简单变量只包含一个元素,一个字符串或一个数字。字符串可能包含任何符号,字母或数字。数字可能包含指数,整数或十进制值。这里标量变量的底线,他们的数据只包含一个单件。你看到的是你得到的标量变量。

以下是标量变量的例子

#!/usr/bin/perl

$number = "5";
$exponent = "2 ** 8";
$string = "Hello, PERL!";
$float = 12.39;

# We can also assign a scalar an empty (undefined) value:
$nothing = undef;

# Printing all the above values - by www.gitbook.net
print "$number\n"; 
print "$exponent\n";
print "$string\n"; 
print "$float\n"; 
print "There is nothing: $nothing\n "; This will give following result 5
2 ** 8
Hello, PERL!
12.39
There is nothing:

标量字符串

字符串是标量,像我们前面提到的,字符串的大小是没有限制的,任何数量的字符,符号或文字,可以补足到字符串。

当定义一个字符串,你可以使用单人或双人的引用,你也可以定义他们使用q子函数。以下是如何定义字符串的例子

$single = 'This string is single quoted';
$double = "This string is double quoted";
$userdefined = q^Carrot is now our quote^;

格式化字符串W /格式化字符

格式化字符串可以根据自己的喜好使用格式字符。其中的一些字符,也可以在Perl中创建的格式文件,认为以下这些字符表示的功能。

Character Description
\L Transform all letters to lowercase
\l Transform the next letter to lowercase
\U Transform all letters to uppercase
\u Transform the next letter to uppercase
\n Begin on a new line
\r Applys a carriage return
\t Applys a tab to the string
\f Applys a formfedd to the string
\b Backspace
\a Bell
\e Escapes the next character
\0nn Creates Octal formatted numbers
\xnn Creates Hexideciamal formatted numbers
\cX Control characters, x may be any character
\Q Backslash (escape) all following non-alphanumeric characters.
\E Ends \U, \L, or \Q functions

下面是几个例子

#!/usr/bin/perl

$newline = "Welcome to \ntutorialspoint.com!";
$small = "\uthis, Here t will become capital !"
$ALLCAPS = "\UThis completely will become capital!";
$PARTIALCAPS = "\UThis half will/E become capital!";
$backslah = "\Q'Hello World'\E!";

print "$newline\n";
print "$small\n";
print "$ALLCAPS\n";
print "$PARTAILCAPS\n";
print "$backslah\n"; This will give following result Welcome to 
tutorialspoint.com!
This, Here t will become capital !
THIS COMPLETELY WILL BECOME CAPITAL!
THIS HALF WILL become capital!
\'Hello World\'!

Substr() 和String索引

substr()函数允许临时更换一个字符串中的字符。我们可以将字符串“Hello,Perl” 更变为 “Hello, World!”很容易的。这意味着我们可以根据索引在数字的字符串的任何字符的每个字符将被自动分配一个数值。 Perl的计数0开头的字符串每个字符的第一个字符和往下排列,直到它到达一个字符串的末尾。

两个参数都必须传到substr()函数,需要要索引和索引号的字符串两个参考。如果两个参数传递,Perl如果要更换的每个字符的结束,那么要从该索引号。

#!/usr/bin/perl

# Define a string to replace
$mystring = "Hello, PERL!";

print "Before replacement : $mystring\n";

substr($mystring, 7) = "World! - www.gitbook.net";

print "After replacement : $mystring\n"; This will give following result Before replacement : Hello, PERL!
After replacement : Hello, World!

因为我们只指定一个字符串的数字参数,我们希望新的字符串每个字符替换后第7个。如果在函数的第三个参数,我们可以更换一个新的字符串,只有我们的字符串一大块。

#!/usr/bin/perl
$mystring = "Hello, PERL!";

print "Before replacement : $mystring\n";

substr($mystring, 3, 6) = "World!";

print "After replacement : $mystring\n"; This will give following result Before replacement : Hello, PERL!
After replacement : HelWorld!RL!

多行字符串

如果你想程序中引入多行字符串,你可以使用标准的引号:

$string = 'This is
a multiline
string';

但是,这是混乱的,是插值和引号使用相同的基本规律。我们可以绕过它使用不同的分隔符Q//QQ//操作符.

另一个更好打印多行的办法

print <

V-Strings

V型字符串可以是一个有用的方式引入到Perl的版本号和IP地址。他们是任何文字与一个V开始,是由一个或多个圆点分隔的元素。例如:

$name = v77.97.114.116.105.110;

数字标量

Perl支持一个相当基本的方法,用于指定一个十进制的数字文本:

$num = 123;      # integer
$num = 123.45;   # floating point
$num = 1.23e45;  # scientific notation

您也可以指定通过指定一个前面的字符的十六进制,八进制和二进制的文字突出显示的号码类型:

$num = 0xff;        # Hexadecimal
$num = 0377;        # Octal
$num = 0b0010_0000; # Binary
您也可以指定通过指定一个前面的字符的十六进制,八进制和二进制的文字突出显示的号码类型。