發新話題

網路管理語言 Perl 入門與實作

3.3 Perl 的命令列選項
除了用前一節的方式執行 Perl script 之外,還可以利用 Perl 的命令列選項:
引用:
[ols3@p4 sample]$ perl -e 'print "Hello Perl !!\n";'
Hello Perl !!
這種方式,主要是利用 -e 選項,將欲執行的述敘放在 '' 單引號之間。

比如,以下方法可以去掉文件中的 ^M 符號:
引用:
perl -pi.bak -e 's/\r//g;' index.html
^M 符號的產生,大多是由於 Windows 平台的文件,在上傳檔案至 Linux/Unix平台的過程中,其換行符號沒有做適當的轉換所致。

以下這個命令列選項可以得知 Perl 的版本代碼:
引用:
perl -v

結果:

This is perl, v5.8.0 built for i386-linux-thread-multi

Copyright 1987-2002, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at 訪客無法瀏覽此圖片或連結,請先 註冊登入會員 , the Perl Home Page.
由上可知,所用的 Perl 版本是 5.8.0

至於 perl -c 這個選項,在前一節中已提到,可用來檢查 Perl script 語法的正確性:
引用:
[ols3@p4 sample]$ perl -c hello.pl
hello.pl syntax OK
Perl 還有非常多的命令列選項,欲知詳情,請下:
引用:
[ols3@p4 perl_intro]$ perl -h

Usage: perl [switches] [--] [programfile] [arguments]
  -0[octal]       specify record separator (\0, if no argument)
  -a              autosplit mode with -n or -p (splits $_ into @F)
  -C              enable native wide character system interfaces
  -c              check syntax only (runs BEGIN and CHECK blocks)
  -d[:debugger]   run program under debugger
  -D[number/list] set debugging flags (argument is a bit mask or alphabets)
  -e 'command'    one line of program (several -e's allowed, omit programfile)
  -F/pattern/     split() pattern for -a switch (//'s are optional)
  -i[extension]   edit <> files in place (makes backup if extension supplied)
  -Idirectory     specify @INC/#include directory (several -I's allowed)
  -l[octal]       enable line ending processing, specifies line terminator
  -[mM][-]module  execute `use/no module...' before executing program
  -n              assume 'while (<>) { ... }' loop around program
  -p              assume loop like -n but print line also, like sed
  -P              run program through C preprocessor before compilation
  -s              enable rudimentary parsing for switches after programfile
  -S              look for programfile using PATH environment variable
  -T              enable tainting checks
  -t              enable tainting warnings
  -u              dump core after parsing program
  -U              allow unsafe operations
  -v              print version, subversion (includes VERY IMPORTANT perl info)
  -V[:variable]   print configuration summary (or a single Config.pm variable)
  -w              enable many useful warnings (RECOMMENDED)
  -W              enable all warnings
  -X              disable all warnings
  -x[directory]   strip off text before #!perl line and perhaps cd to director
除了 -e -c -v -h 之外,上述選項中比較常用的還有 -w (打開警告訊息) -T (安全檢查),往後我們會再加以說明。

TOP

4. Perl 的資料型態
這一章簡單介紹 Perl 的三種資料型態:

三種資料型態

1.純量(scalars)

2.陣列(array)

3.雜湊(hash)

熟悉這三種資料,是抓住 Perl 精髓的開始。

TOP

4.1 純量(scalars)
純量是 Perl 能夠操作的資料型態中最簡單的一種。

Perl 的純量由數值及字串組成。對其它程式語言來說,數值和字串的處理方式是不同的,但對 Perl 來說,則沒有太大的不同。


註:Perl 的純量還有一種稱為:參考 (reference),但因屬進階範圍,在此暫不說明。請參考:第 9 章。

TOP

4.1.1 數值
以下是 Perl 的數值種類:
引用:
整數:

1
0
566
-80
37999


浮點數:

3.141573259
4.000
1.96e24
-4.5E-18


八進位數:(以 0 開頭)

0523 (相當於十進位數:339)


十六進位數:(以 0x 開頭)

0x27af (相當於十進位數:10159)


二進位數:(以 0b 開頭)

0b11010011 (相當於十進位數:211)

TOP

4.1.2 數值計算
以下是 Perl 常用的數值運算符號:
引用:
加  +         例:5 + 9
減  -         例:100.9 - 10.2
乘  *         例:7 * 8
除  /         例:56 / 7
求餘數 %       例:41 % 2
乘冪 **       例:2 ** 4  (2 的 4 次方 = 16)

TOP

4.1.3 運算子
運算子(Operators)對照表,運算優先順序越往下越低:
引用:
-> 方法呼叫、解參考
++ 遞增, -- 遞減
** 乘冪(次方)
! 非,~ 位元運算的 非,\ 參考
=~ 比對相符,!~ 比對不符
* 乘,/ 除, % 求餘數, x 字串倍數
+ 加、- 減, . 字串連接
<< 位元左移,>> 位元右移
< 小於,> 大於,<= 小於或等於,>= 大於或等於,lt 字串小於,gt 字串大於,le 字串小於或等於,ge 字串大於或等於
== 等於,!= 不等於,<=> 數值比較,eq 字串等於,ne 字串不等於,cmp 字串比較
& 位元AND
| 位元OR,^ 位元 XOR
&& AND(且)
|| OR (或)
.. 範圍
? : 三元運算子,例: ($a > $b) ? $a : $b;
= 指定,+=、-=,*= 等等
, 逗號運算子,=> 箭號運算子
not 邏輯的 NOT
and 邏輯的 AND
or  邏輯的 OR
xor 邏輯的 XOR
這裡有更詳細的說明,請參考:perlop.html

如果運算子作用的對象(稱為運算元)個數為 2 個,則該運算子稱為二元運算子,如:+ - * / 等;如果作用的對象為 1 個,則稱為單元運算子,如 ++ --;如果作用的對象為 3 個,則稱為三元運算子,如 ? :

TOP

4.1.4 字串
字串由任何字元所組成,Perl 的字串須以單引號或雙引號括起來:
引用:
'John and Marry'
'真的好!'
'12345 大家來跳舞'
'hello world\n'

"中文沒問題"
"hello world\n"

TOP

4.1.5 跳脫字元(escapes)
所謂跳脫字元是指該字元具有特殊涵義,必須特別對待,通常以倒斜線加上某一固定字元來表示,以下是 Perl 常見的跳脫字元:
引用:
\n   換行符號
\r   回行符號
\t   定位 tab 符號
\f   跳頁
\b   退格
\a   鈴聲
\0   八進位數,如:\007
\x   十六進位數,如:\x16
\\   \ 這個符號
\"   " 雙引號
這裡有更詳細的說明,請參考:perlop.html

TOP

4.1.6 單引號和雙引號的區別
先前提到:字串必須用單引號和雙引號括起來,那麼二者之間,有沒有什麼不同?

有! 至少有二個地方不一樣:

1. 跳脫符號在雙引號中才有效,在單引號中無效。
引用:
比如:

print "hello world\n";

出現 hello world 並且游標換至下一列




print 'hello world\n';

出現 hello world\n 且游標沒有換行。
2. 雙引號中可以做變數代換,單引號則不行。
引用:
比如:

$hi="Hello";

print "$hi\n";

出現 Hello 並且游標換至下一列




$hi="Hello";

print '$hi\n';


出現 $hi\n 且游標沒有換行。

TOP

4.1.7 字串的運算
所謂字串的運算,通常指的是把二個或二個以的字串結合起來,其運算符號是 .
引用:
print '12345 大家來跳舞' . " hello world";

結果變成:

12345 大家來跳舞 hello world
字串有另一種倍數的運算,其算符是 x,用法如下:
引用:
print "OK" x 4;

結果變成:

OKOKOKOK
也就是說:將四份 OK 字串串連在一起。

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。