博客
关于我
Perl的基本語法
阅读量:795 次
发布时间:2023-02-27

本文共 16914 字,大约阅读时间需要 56 分钟。

前言

這篇文章是花了我很多時間、費了我很多心血才完成的,雖然連我自己都覺得無法達到盡善盡美的境界,但希望能幫助大家入門,稍微了解到Perl到底是個什麼樣的東西,Perl到底有那些強大的功能,那麼這篇文章的目的就達到了。

(1) 資料型態

Perl的資料型態大致分為四種:Scalar、Scalar Array、Hash Array(相關陣列)和References(參考指標)。雖然用起來卻綽綽有餘。尤其在寫Perl程式時可以不必事先宣告變數,這一點對剛學程式語言的人甚為方便,不過為了以後程式除錯和維護方便,我建議你還是養成事先宣告變數的習慣比較好。

(1)(a) Scalar

純量變數是Perl裡最基本的一種資料型態,它可以代表一個字元、字串、整數、甚至浮點數,而Perl把它們都看成是一樣的東東! 你甚至可以混著用,不可思議吧。例如:

  • # 井字號開頭的後面都是註解。
  • # 純量變數以$開頭。
  • # my 是一種宣告變數的方式,它可以使變數區域化。
  • # 宣告變數時若不加 my 或 local 則Perl會把它當作全域變數使用。
  • # 習慣上,我們會將字串用雙引号括起來,而數值就不用加引号。

my $x="abc";

my $x=123;
my $x=4.56;

那麼程式怎麼判斷這是數值還是字串呢? 其实不是程式判斷,而是你自己要判斷。Perl分別提供了一堆運算子來處理數字和字串,你必須知道這個變數是數值或字串,才能使用個別的運算子來對變數做運算。我分別列出字串運算子和數值運算子,好讓大家能區分它們的不同。

◎ 字串運算子

String Operator Purpose
x Returns a string consisting of the string on the left of the operand, repeated the number of times of the right operand.
Concatenates the two strings on both sides of the operator.
eq Returns True if the two operands are equivalent, False otherwise.
ne Returns True if the two operands are not equal, False otherwise.
le Returns True if the operand on the left is stringwise less than the operand on the right of the operator. Returns False otherwise.
lt Returns True if the operand on the left is stringwise less than or equal to the operand on the right of the operator. Returns False otherwise.
ge Returns True if the operand on the left is stringwise greater than or equal to the operand on the right of the operator. Returns False otherwise.
gt Returns True if the operand on the left is stringwise greater than the operand on the right of the operator. Returns False otherwise.
cmp Returns -1, 0, or 1 if the left operand is stringwise less than, equal to, or greater than the right operand.
, Evaluates the left operand, the evaluates the right operand. It returns the result of the right operand.
++ Increments the string by one alphabetic value.

◎ 數值運算子

Value Operator Purpose
+ Computes the additive value of the two operands.
- Computes the difference between the two operands.
* Computes the multiplication of the two operands.
/ Computes the division between the two operands.
% Computes the modulus(remainder) of the two operands.
= Returns Ture if the two operands are equivalent, False otherwise.
!= Returns Ture if the two operands are not equal, False otherwise.
<= Returns Ture if the operand on the left is numerically less than or equal to the operand on the right of the operator. Returns False otherwise.
>= Returns Ture if the operand on the left is numerically greater than or equal to the operand on the right of the operator. Returns False otherwise.
< Returns Ture if the operand on the left is numerically less than the operand on the right of the operator. Returns False otherwise.
> Returns Ture if the operand on the left is numerically greater than the operand on the right of the operator. Returns False otherwise.
<= > Returns -1 if the left operand is less than the right, +1 if is it greater than, and 0(False) otherwise.
&& Performs a logical AND operation. If the left operand is True m then the right operator is not evaluated.
|| Performs a logical OR operation. If the left operand is True m then the right operator is not evaluated.
& Returns the valueof the two operators bitwise ANDed.
| Returns the valueof the two operators bitwise ORed.
^ Returns the valueof the two operators bitwise XORed.
++ Increment operator. Increments the variable's value by 1.
-- Decrement operator. Decrements the variable's value by 1.
** Computes the power of the left-hand value to the power of the rihght-hand value.
+= Adds the value of the right-hand operand to the value of the left-hand operand.
-+ Subtracts the value of the right-hand operand to the value of the left-hand operand.
*= Mlutiplies the value of the left-hand operand to the value of the right-hand operand.
>> Shifts the left operand right by the number of bits that is specified by the right operand.
<< Shifts the left operand left by the number of bits that is specified by the right operand.
~ Performs a 1s complement of the operator. This is a unary operator.

(1)(b) Scalar Array

純量陣列,陣列內的每一個元素都是Scalar variable。宣告及使用方式如下:

  • # 純量陣列以 @ 開頭。
  • my ;
  • my @array=qw(a b c d);
  • my @array=("a","b","c","d");
  • $array[0]="a"; $array[1]="b"; $array[2]="c"; $array[3]="d";
  • for($i=0; $i<=$#array; $i++) { print "$array[$i]\n"; }

看到了$#array這個奇怪的東東沒? 這是Perl的一個特殊用法,代表這個陣列最後一個元素的註標。 由於Perl不必事先宣告變數,也不必預先宣告陣列的大小,甚至可以隨時增加新元素,那我們怎麼知道這個陣列到底有多大呢? 透過這個特殊變數我們可以得知這個陣列究竟有多大了。

(1)(c) Hash Array

雜湊陣列也叫做相關陣列,它和一般陣列沒什麼不同,差別只是在它的索引值用的是字串,而非一般陣列所用的整數值, 因此相關陣列不像一般陣列一樣有次序的概念,它沒有所謂的第一項資料這種說法。它就相當於把一堆變數組合成一個group,然後我們可以透過索引字串存取這個group每一個元素的值。 相關陣列的宣告及使用方式如下:

  • # 相關陣列是以 % 符号開頭的。
  • my %hash;
  • my %hash=("i1"=>"aaa","i2"=>"bbb","i3"=>"ccc");
  • my %hash=("i1","aaa","i2","bbb","i3","ccc");
  • $hash{'i1'}="aaa"; $hash{'i2'}="bbb"; $hash{'i3'}="ccc";
  • foreach $key (keys %hash) { print "$hash{$key}\n"; }
  • foreach $value (values %hash) { ... }
  • while(($key,$value)=each %hash) { ... }

(1)(d) References

Perl 5新增了參考指標的資料型態,使Perl和C一樣可借由指標建立一些複雜的資料結構。 普通程式是用不到指標這玩意的,下面也只是簡單介紹一下,看不懂的人可不必深究。

⊙ 如何取得變數的位址?

  • $scalarRef=\$scalarVar;
  • $arrayRef=\@arrayVar;
  • $hashRef=\%hashVar;
  • $funcRef=\&funcName;

⊙ 如何使用指標?

  • print $$scalarRef;
  • print "@$arrayRef";
  • print $hashRef->{$key};
  • &$funcRef;

(2) 控制敘述

(a) Conditional Control Statements

Perl的條件控制敘述和C語言很像,讓使用者很快就能掌握它。不過Perl比C語言又另外多了些實用的語法:

  • # Expression 就是條件敘述式,Perl和C一樣沒有定義布林資料型態(Boolean data type),因此 0 是false、非0 是ture。
  • # Code Segment 就是用大括號括起來的一堆指令,也就是一個Block。
  • if (Expression) {Code Segment}
  • if (Expression) {Code Segment} else {Code Segment}
  • if (Expression) {Code Segment} elsif (Expression) {Code Segment} else {Code Segment}
  • statement if (Expression);
  • statement unless (Expression);

例:

print "HELLO!\n" if ($name eq "friend");

$x -= 10 if ($x == 100);

(b) Loop Control Statements

  • # 注意:純量變數前面要加個 $ 字號,這一點和C語言不一樣哦。
  • for($i=0; $i<=10; $i++) {Code Segment}
  • foreach $i (@array) {Code Segment}
  • for $i (0..10) {Code Segment}
  • while($i<=10) {Code Segment}
  • do {Code Segment} while(Expression);
  • last 是跳出現在所在的迴圈,next則是跳過下面的指令直接執行下一次的迴圈。

例:

while(chomp($i=<STDIN>)) { next if ($i == 5); last unless ($i > 10); }

(3) 副程式

(a) Syntax: sub NAME {Code}

(b) 呼叫副程式: &NAME(para1, para2,...)

(c) 參數傳遞: @_ Perl和C一樣是採用Call by value的方式,不過因為Perl不用事先宣告變數,所以建立副程式的時候也不用宣告要傳遞什麼參數。

  • my ($a1,$a2,$a3,...) = @_;
  • my @arg=@_;
  • my %arg=@_;

(d) Variable Localization: my or local

  • my $x=3;
  • local $x=3;

(4) I/O和檔案處理

(a) Syntax: open(FILEHANDLE,"Expression"); close(FILEHANDLE);

Expression Effect
open(FH, "<filename") Opens filename for reading.
open(FH, "+<filename") Opens filename for both reading and writing.
open(FH, ">filename") Opens filename for writing.
open(FH, "+>filename") Opens filename for both reading and writing.
open(FH, ">>filename") Appends to filename.
open(FH, "command|") Runs the command and pipes its output to the filehandle.
open(FH, "command|") Pipes the output along the filehandle to the command.
open(FH, "-") Opens STDIN.
open(FH, ">-") Opens STDOUT.
open(FH, "<&=N") Opens filename for reading.
open(FH, ">&=N") Opens filename for writing.

例:

open(FILE, $filename) || die "Can't open file $filename : $!\n";

print while(<FILE>);

close(FILE);

(b) Input

Perl沒有特別用來輸入的函數,因為Perl在執行程式時,會自動開啟標準輸入裝置,其filehandle定為STDIN,所以在Perl中要輸入資料的方法就是使用<STDIN>:

chomp $input=<STDIN>;

chomp($input=<STDIN>);

(c) Output

  • print "variables or 字串";
  • print "Scalar value is $x\n";
  • print "$x";
  • print "$x";
  • print `$x`;

(5) 正則表達式

(a) Modifiers

Modifier Purpose
g Match globally, i.e. find all occurrences.
i Makes the search case-insensitive.
m Treat string as multiple lines.
o Only compile pattern once.
s Treat string as single line.
x Use extended regular expressions.

(b) Metacharacter

Metacharacter Purpose
\ Tells Perl to accept the following characters as a regular character; this removes special meanings from any metacharacter.
^ Matches the beginning of the string, unless /m is used.
. Matches any character except a new line character, unless /s is used.
$ Matches the end of the string, unless /m is used.
| Expresses alternation. This means the expressions will search for multiple patterns in the same string.
( ) Groups expressions to assist in alternation and back referencing.
[ ] Looks for a set of characters.

(c) Pattern Quantifier

Pattern Quantifier Purpose
* Matchs 0 or more times.
+ Matchs 1 or more times.
? Matchs 0 or 1 times.
{n} Matches exactly n times.
{n,} Matches at least n times.
{n,m} Matches at least n times but no more than m times.

(d) Character Patterns

Character Patterns Purpose
\r Carriage return(CR), ASCII 13(十進位)
\n New line, UNIX中代表ASCII 10(十進位), DOS(Windows)系統中則是ASCII 13 + ASCII 10(十進位).
\t Tab, ASCII 9(十進位)
\w Matches an alphanumeric character. 即 [A-Za-z0-9_].
\W Matches a nonalphanumeric character. 即 [^A-Za-z0-9_].
\s Matches a white space character. 即 [\ \t\f\r\n].
\S Matches a non-whote space character. 即 [^\ \t\f\r\n].
\d Matches a digit. 即 [0-9].
\D Matches a nondigit character. 即 [^0-9].
\b Matches a word boundary.
\B Matches a nonword boundary.
\033 octal char
\x1B hex char

(e) Examples

  • /abc/
  • ^abc$
  • a|b
  • ab{2,4}c
  • ab*c
  • ab+c
  • a.c
  • [abc]
  • \d
  • \w
  • \s
  • \S
  • \d
  • \D
  • \b
  • \B
  • \033
  • \x1B

(6) 特殊變數

特殊變數 描述
$_ The default input and pattern-searching space.
$digit Contains the subpattern from a successful parentheses pattern match.
$. The current input line number of last filehandle read.
$! Contains the current value of errno.
$0 The name of the file of the Perl script.
@ARGV The command line arguments issued when the script was started.
@_ The parameter array for subroutines.
%ENV This associative array contains your current environment.

转载地址:http://zmvfk.baihongyu.com/

你可能感兴趣的文章