顯示具有 perl 標籤的文章。 顯示所有文章
顯示具有 perl 標籤的文章。 顯示所有文章

2016年9月17日 星期六

perl quotes

Alternative Quotes: qq, q, qw, qx

print qq/Hello\n/;                  # same as: print "Hello\n";
print q/He owes $5.00/;       #  same as: print 'He owes $5.00', "\n";
@states=qw( E T A L );       # same as ("E", "T", "A","L")
$today = qx(date);               # same as $today = 'date'; 



While we usually think of quotes as literal values,
in Perl they function as operators,
providing various kinds of interpolating and pattern matching capabilities.
Perl provides customary quote characters for these behaviors,
but also provides a way for you to choose your quote character for any of them.
In the following table, a "{}" represents any pair of delimiters you choose.

Customary  Generic        Meaning        Interpolates
                 ''       q{}          Literal             no
               ""      qq{}          Literal             yes
               ``      qx{}          Command             yes*
                        qw{}         Word list            no
                //       m{}       Pattern match          yes*
                         qr{}          Pattern             yes*
                         s{}{}      Substitution          yes*
                         tr{}{}    Transliteration         no (but see below)
              <<EOF                 here-doc            yes*

    * unless the delimiter is ''.



perl operators and precedence


operators
+
-
*
/

precedence
4+2*4
(4+2)*4

notice:  ++ different location different result
print $num1++ . "\n";
result: 10

print $num1;
11

print ++$num2 . "\n";
11



$letter  = "a";
$letter++;
print  "$letter\n";





perl conditionals

loop,foreach

if elsif else

unless

perl arrays

array

push

pop

shift

merge

reverse

sort

qw



perl variables

$a = 1;

($a , $b) = (1,2);

$c = $a + $b ;

print "$a\n";
print "$b\n";
result: 1 2

print "$c\n";
result: 3

notice:
print "$a + $b \n";
result: 1 + 2

$str = "Hello Wrold!\n";
print "$str\n";
result: Hello World!


my $name = 'Foo';
print 'Hello $name, how are you?\n';
Single quoted result:  Hello $name, how are you?\n

print "Hello $name, how are you?\n";
Double quoted result:  Hello Foo, how are you?



notice:
$name = 'Foo'; the same as $name = "Foo";
\  在許多程式語言解釋為跳脫字元。
\n 在許多程式語言解釋為換行字元。
quoted裡的元件基本上會當作字串處理,包含實際字串和便數字串。所以 print "$string"可以,但是如果是print "$strint++\n"就不行。要使用print $sting++."\n"


perl basics

1. 由shebang (#!) 開始

#!/usr/bin/perl

類 Unix 作業系統的程式載入器會分析 Shebang 後的內容,將這些內容作為直譯器指令,並呼叫該指令,並將載有 Shebang 的檔案路徑作為該直譯器的參數。


 2. 以browser呼叫執行傳回最少要加上

print "Content-Type: text/html; charset=\"UTF-8\"\n\n";


3. exit;


example code:

#!/usr/bin/perl

print "Content-Type: text/html; charset=\"UTF-8\"\n\n";

print "Hello World!\n";

exit;

result:

Hello World !