Perl的基本语法Perl的基本语法Word文档下载推荐.docx
《Perl的基本语法Perl的基本语法Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Perl的基本语法Perl的基本语法Word文档下载推荐.docx(21页珍藏版)》请在冰豆网上搜索。
![Perl的基本语法Perl的基本语法Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2023-1/6/278763e0-fbf2-496f-b23a-2e0aee0231f5/278763e0-fbf2-496f-b23a-2e0aee0231f51.gif)
純量變數是Perl裡最基本的一種資料型態,它可以代表一個字元、字串、整數、甚至浮點數,而Perl把它們都看成是一樣的東東!
你甚至可以混著用,不可思議吧。
标量标量是Perl里最基本的一种资料型态,它可以代表一个字元、字串、整数、甚至浮点数,而Perl把它们都看成是一样的东东!
你甚至可以混着用,不可思议吧。
例如:
例如:
#井字號開頭的後面都是註解。
#純量變數以$開頭。
#my是一種宣告變數的方式,它可以使變數區域化。
#宣告變數時若不加my或local則Perl會把它當作全域變數使用。
#習慣上,我們會將字串用雙引號括起來,而數值就不用加引號。
my$x="
abc"
;
my$x=123;
my$x=4.56;
那麼程式怎麼判斷這是數值還是字串呢?
其實不是程式判斷,而是你自己要判斷。
那么程式怎么判断这是数值还是字串呢?
其实不是程式判断,而是你自己要判断。
Perl分別提供了一堆運算子來處理數字和字串,你必須知道這個變數是數值或字串,才能使用個別的運算子來對變數做運算。
Perl分别提供了一堆运算子来处理数字和字串,你必须知道这个变数是数值或字串,才能使用个别的运算子来对变数做运算。
我分別列出字串運算子和數值運算子,好讓大家能區分它們的不同。
我分别列出字串运算子和数值运算子,好让大家能区分它们的不同。
◎字串運算子◎字串运算子
StringOperatorStringOperator
PurposePurpose
xx
Returnsastringconsistingofthestringontheleftoftheoperand,repeatedthenumberoftimesoftherightoperand.Returnsastringconsistingofthestringontheleftoftheoperand,repeatedthenumberoftimesoftherightoperand.
..
Concatenatesthetwostringsonbothsidesoftheoperator.Concatenatesthetwostringsonbothsidesoftheoperator.
eqeq
ReturnsTrueifthetwooperandsareequivalent,Falseotherwise.ReturnsTrueifthetwooperandsareequivalent,Falseotherwise.
nene
ReturnsTrueifthetwooperandsarenotequal,Falseotherwise.ReturnsTrueifthetwooperandsarenotequal,Falseotherwise.
lele
ReturnsTrueiftheoperandontheleftisstringwiselessthantheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTrueiftheoperandontheleftisstringwiselessthantheoperandontherightoftheoperator.ReturnsFalseotherwise.
ltlt
ReturnsTrueiftheoperandontheleftisstringwiselessthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTrueiftheoperandontheleftisstringwiselessthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.
gege
ReturnsTrueiftheoperandontheleftisstringwisegreaterthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTrueiftheoperandontheleftisstringwisegreaterthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.
gtgt
ReturnsTrueiftheoperandontheleftisstringwisegreaterthantheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTrueiftheoperandontheleftisstringwisegreaterthantheoperandontherightoftheoperator.ReturnsFalseotherwise.
cmpcmp
Returns-1,0,or1iftheleftoperandisstringwiselessthan,equalto,orgreaterthantherightoperand.Returns-1,0,or1iftheleftoperandisstringwiselessthan,equalto,orgreaterthantherightoperand.
,
Evaluatestheleftoperand,theevaluatestherightoperand.Itreturnstheresultoftherightoperand.Evaluatestheleftoperand,theevaluatestherightoperand.Itreturnstheresultoftherightoperand.
++++
Incrementsthestringbyonealphabeticvalue.Incrementsthestringbyonealphabeticvalue.
◎數值運算子◎数值运算子
ValueOperatorValueOperator
++
Computestheadditivevalueofthetwooperands.Computestheadditivevalueofthetwooperands.
--
Computesthedifferencebetweenthetwooperands.Computesthedifferencebetweenthetwooperands.
**
Computesthemultiplicationofthetwooperands.Computesthemultiplicationofthetwooperands.
//
Computesthedivisionbetweenthetwooperands.Computesthedivisionbetweenthetwooperands.
%%
Computesthemodulus(remainder)ofthetwooperands.Computesthemodulus(remainder)ofthetwooperands.
====
ReturnsTureifthetwooperandsareequivalent,Falseotherwise.ReturnsTureifthetwooperandsareequivalent,Falseotherwise.
!
=!
=
ReturnsTureifthetwooperandsarenotequal,Falseotherwise.ReturnsTureifthetwooperandsarenotequal,Falseotherwise.
<
=<
ReturnsTureiftheoperandontheleftisnumericallylessthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTureiftheoperandontheleftisnumericallylessthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.
=>
=>
ReturnsTureiftheoperandontheleftisnumericallygreaterthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTureiftheoperandontheleftisnumericallygreaterthanorequaltotheoperandontherightoftheoperator.ReturnsFalseotherwise.
<
ReturnsTureiftheoperandontheleftisnumericallylessthantheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTureiftheoperandontheleftisnumericallylessthantheoperandontherightoftheoperator.ReturnsFalseotherwise.
>
>
ReturnsTureiftheoperandontheleftisnumericallygreaterthantheoperandontherightoftheoperator.ReturnsFalseotherwise.ReturnsTureiftheoperandontheleftisnumericallygreaterthantheoperandontherightoftheoperator.ReturnsFalseotherwise.
=>
Returns-1iftheleftoperandislessthantheright,+1ifisitgreaterthan,and0(False)otherwise.Returns-1iftheleftoperandislessthantheright,+1ifisitgreaterthan,and0(False)otherwise.
&
&
PerformsalogicalANDoperation.IftheleftoperandisTruemthentherightoperatorisnotevaluated.PerformsalogicalANDoperation.IftheleftoperandisTruemthentherightoperatorisnotevaluated.
||||
PerformsalogicalORoperation.IftheleftoperandisTruemthentherightoperatorisnotevaluated.PerformsalogicalORoperation.IftheleftoperandisTruemthentherightoperatorisnotevaluated.
ReturnsthevalueofthetwooperatorsbitwiseANDed.ReturnsthevalueofthetwooperatorsbitwiseANDed.
||
ReturnsthevalueofthetwooperatorsbitwiseORed.ReturnsthevalueofthetwooperatorsbitwiseORed.
^^
ReturnsthevalueofthetwooperatorsbitwiseXORed.ReturnsthevalueofthetwooperatorsbitwiseXORed.
Incrementoperator.Incrementsthevariable'
svalueby1.Incrementoperator.Incrementsthevariable'
svalueby1.
----
Decrementoperator.Decrementsthevariable'
svalueby1.Decrementoperator.Decrementsthevariable'
****
Computesthepoweroftheleft-handvaluetothepoweroftherihght-handvalue.Computesthepoweroftheleft-handvaluetothepoweroftherihght-handvalue.
+=+=
Addsthevalueoftheright-handoperandtothevalueoftheleft-handoperand.Addsthevalueoftheright-handoperandtothevalueoftheleft-handoperand.
-+-+
Subtractsthevalueoftheright-handoperandtothevalueoftheleft-handoperand.Subtractsthevalueoftheright-handoperandtothevalueoftheleft-handoperand.
*=*=
Mlutipliesthevalueoftheleft-handoperandtothevalueoftheright-handoperand.Mlutipliesthevalueoftheleft-handoperandtothevalueoftheright-handoperand.
Shiftstheleftoperandrightbythenumberofbitsthatisspecifiedbytherightoperand.Shiftstheleftoperandrightbythenumberofbitsthatisspecifiedbytherightoperand.
Shiftstheleftoperandleftbythenumberofbitsthatisspecifiedbytherightoperand.Shiftstheleftoperandleftbythenumberofbitsthatisspecifiedbytherightoperand.
~~
Performsa1scomplementoftheoperator.Thisisaunaryoperator.Performsa1scomplementoftheoperator.Thisisaunaryoperator.
(b)ScalarArray:
(b)ScalarArray:
純量陣列,陣列內的每一個元素都是Scalarvariable。
纯量阵列,阵列内的每一个元素都是Scalarvariable。
宣告及使用方式如下:
宣告及使用方式如下:
#純量陣列以@開頭。
my@array;
my@array=qw(abcd);
#qw函數會將其後的每個元素用逗點隔開,效果就像下面這行。
my@array=("
a"
"
b"
c"
d"
);
#當然你也可以一個個元素宣告,下面就是存取每一個元素的方法。
#因為陣列中的每一個元素都是純量變數,所以要以$開頭,
#剛開始容易搞混,請注意。
$array[0]="
$array[1]="
$array[2]="
$array[3]="
#使用forloop印出陣列內每個元素的值。
for($i=0;
$i<
=$#array;
$i++){
print"
$array[$i]\n"
}
看到$#array這個奇怪的東東沒?
這是Perl的一個特殊用法,代表這個陣列最後一個元素的註標。
看到$#array这个奇怪的东东没?
这是Perl的一个特殊用法,代表这个阵列最后一个元素的注标。
由於Perl不必事先宣告變數,也不必預先宣告陣列的大小,甚至可以隨時增加新元素,那我們怎麼知道這個陣列到底有多大呢?
透過這個特殊變數我們可以得知這個這個陣列最後一個元素的註標,自然而然也就知道這個陣列究竟有多大了。
由于Perl不必事先宣告变数,也不必预先宣告阵列的大小,甚至可以随时增加新元素,那我们怎么知道这个阵列到底有多大呢?
透过这个特殊变数我们可以得知这个这个阵列最后一个元素的注标,自然而然也就知道这个阵列究竟有多大了。
另外Perl只定義了一維陣列的語法,二維以上只能用指標間接來達成。
另外Perl只定义了一维阵列的语法,二维以上只能用指标间接来达成。
(c)HashArray(AssociativeArray):
(c)HashArray(AssociativeArray):
雜湊陣列也叫做相關陣列,它和一般陣列沒什麼不同,差別只是在它的索引值用的是字串,而非一般陣列所用的整數值,因此相關陣列不像一般陣列一樣有次序的概念,它沒有所謂的第一項資料這種說法。
杂凑阵列也叫做相关阵列,它和一般阵列没什么不同,差别只是在它的索引值用的是字串,而非一般阵列所用的整数值,因此相关阵列不像一般阵列一样有次序的概念,它没有所谓的第一项资料这种说法。
它就相當於把一堆變數組合成一個group,然後我們可以透過索引字串存取這個group每一個元素的值。
它就相当于把一堆变数组合成一个group,然后我们可以透过索引字串存取这个group每一个元素的值。
相關陣列的宣告及使用方式如下:
相关阵列的宣告及使用方式如下:
#相關陣列是以%符號開頭的。
my%hash;
#=>
這個符號是Perl5新增的,是為了相關陣列量身定做的,
#因為索引和元素值都是純量,若使用=>
這個符號,
#(索引=>
元素值)兩兩對應,就不容易發生失誤。
my%hash=("
i1"
"
aaa"
i2"
bbb"
i3"
ccc"
#上面這行的效果和下面這行是一樣的。
a