$ program3_3 first value is 0 second value is 0.01 三、字符串 惯用C的程序员要注意,在PERL中,字符串的末尾并不含有隐含的NULL字符,NULL字符可以出现在串的任何位置。 . 双引号内的字符串中支持简单变量替换,例如: $number = 11; $text = "This text contains the number $number."; 则$text的内容为:"This text contains the number 11."
.双引号内的字符串中支持转义字符 Table 3.1. Escape sequences in strings.
Escape Sequence Description \a Bell (beep) \b Backspace \cn The Ctrl+n character \e Escape \E Ends the effect of \L, \U or \Q \f Form feed \l Forces the next letter into lowercase \L All following letters are lowercase \n Newline \r Carriage return \Q Do not look for special pattern characters \t Tab \u Force next letter into uppercase \U All following letters are uppercase \v Vertical tab
\L、\U、\Q功能可以由\E关闭掉,如: $a = "T\LHIS IS A \ESTRING"; # same as "This is a STRING"
.要在字符串中包含双引号或反斜线,则在其前加一个反斜线,反斜线还可以取消变量替换,如: $res = "A quote \" and A backslash \\"; $result = 14; print ("The value of \$result is $result.\n")的结果为: The value of $result is 14.
.可用\nnn(8进制)或\xnn(16进制)来表示ASCII字符,如: $result = "\377"; # this is the character 255,or EOF $result = "\xff"; # this is also 255
.单引号字符串 单引号字符串与双引号字符串有两个区别,一是没有变量替换功能,二是反斜线不支持转义字符,而只在包含单引号和反斜线时起作用。单引号另一个特性是可以跨多行,如: $text = 'This is two lines of text '; 与下句等效: $text = "This is two\nlines of text\n";
操作符 描述 = Assignment only += Addition and assignment -= Subtraction and assignment *= Multiplication and assignment /= Division and assignment %= Remainder and assignment **= Exponentiation and assignment &= Bitwise AND and assignment |= Bitwise OR and assignment ^= Bitwise XOR and assignment
Here is a line of Input. This Input contains some Capitalized words. ^D Capitalized words and number of occurrences: Here: 1 Input: 2 This: 1 Capitalized: 1
Here is a line of Input. This Input contains some Capitalized words. ^D Capitalized words and number of occurrences: This: 1 Input: 2 Here: 1 Capitalized: 1
1 : #!/usr/local/bin/perl 2 : 3 : # initialize list to empty 4 : $header = ""; 5 : while ($line = ) { 6 : # remove leading and trailing spaces 7 : $line =~ s/^s+|s+$//g; 8 : @words = split(/s+/, $line); 9 : foreach $word (@words) { 10: # remove closing punctuation, if any 11: $word =~ s/[.,;:-]$//; 12: # convert all words to lower case 13: $word =~ tr/A-Z/a-z/; 14: &add_word_to_list($word); 15: } 16: } 17: &print_list; 18: 19: sub add_word_to_list { 20: local($word) = @_; 21: local($pointer); 22: 23: # if list is empty, add first item 24: if ($header eq "") { 25: $header = $word; 26: $wordlist{$word} = ""; 27: return; 28: } 29: # if word identical to first element in list, 30: # do nothing 31: return if ($header eq $word); 32: # see whether word should be the new 33: # first word in the list 34: if ($header gt $word) { 35: $wordlist{$word} = $header; 36: $header = $word; 37: return; 38: } 39: # find place where word belongs 40: $pointer = $header; 41: while ($wordlist{$pointer} ne "" && 42: $wordlist{$pointer} lt $word) { 43: $pointer = $wordlist{$pointer}; 44: } 45: # if word already seen, do nothing 46: return if ($word eq $wordlist{$pointer}); 47: $wordlist{$word} = $wordlist{$pointer}; 48: $wordlist{$pointer} = $word; 49: } 50: 51: sub print_list { 52: local ($pointer); 53: print ("Words in this file:n"); 54: $pointer = $header; 55: while ($pointer ne "") { 56: print ("$pointern"); 57: $pointer = $wordlist{$pointer}; 58: } 59: }
运行结果如下:
Here are some words. Here are more words. Here are still more words. ^D Words in this file: are here more some still words
#!/usr/bin/perl 1 # 2 # Using Associative Array references 3 # 4 %month = ( 5 '01', 'Jan', 6 '02', 'Feb', 7 '03', 'Mar', 8 '04', 'Apr', 9 '05', 'May', 10 '06', 'Jun', 11 '07', 'Jul', 12 '08', 'Aug', 13 '09', 'Sep', 14 '10', 'Oct', 15 '11', 'Nov', 16 '12', 'Dec', 17 ); 18 19 $pointer = %month; 20 21 printf "n Address of hash = $pointern "; 22 23 # 24 # The following lines would be used to print out the 25 # contents of the associative array if %month was used. 26 # 27 # foreach $i (sort keys %month) { 28 # printf "n $i $$pointer{$i} "; 29 # } 30 31 # 32 # The reference to the associative array via $pointer 33 # 34 foreach $i (sort keys %$pointer) { 35 printf "$i is $$pointer{$i} n"; 36 }
结果输出如下:
$ mth Address of hash = HASH(0x806c52c) 01 is Jan 02 is Feb 03 is Mar 04 is Apr 05 is May 06 is Jun 07 is Jul 08 is Aug 09 is Sep 10 is Oct 11 is Nov 12 is Dec
1 #!/usr/bin/perl 2 # 3 # Using Array references 4 # 5 %weekday = ( 6 '01' => 'Mon', 7 '02' => 'Tue', 8 '03' => 'Wed', 9 '04' => 'Thu', 10 '05' => 'Fri', 11 '06' => 'Sat', 12 '07' => 'Sun', 13 ); 14 $pointer = %weekday; 15 $i = '05'; 16 printf "n ================== start test ================= n"; 17 # 18 # These next two lines should show an output 19 # 20 printf '$$pointer{$i} is '; 21 printf "$$pointer{$i} n"; 22 printf '${$pointer}{$i} is '; 23 printf "${$pointer}{$i} n"; 24 printf '$pointer->{$i} is '; 25 26 printf "$pointer->{$i}n"; 27 # 28 # These next two lines should not show anything 29 # 30 printf '${$pointer{$i}} is '; 31 printf "${$pointer{$i}} n"; 32 printf '${$pointer->{$i}} is '; 33 printf "${$pointer->{$i}}"; 34 printf "n ================== end of test ================= n"; 35
结果输出如下:
================== start test ================= $$pointer{$i} is Fri ${$pointer}{$i} is Fri $pointer->{$i} is Fri ${$pointer{$i}} is ${$pointer->{$i}} is ================== end of test =================
#!/usr/bin/perl sub errorMsg { my $lvl = shift; # # define the subroutine to run when called. # return sub { my $msg = shift; # Define the error type now. print "Err Level $lvlmsgn"; }; # print later. } $severe = errorMsg("Severe"); $fatal = errorMsg("Fatal"); $annoy = errorMsg("Annoying");
&$severe("Divide by zero"); &$fatal("Did you forget to use a semi-colon?"); &$annoy("Uninitialized variable in use");
结果输出如下:
Err Level Severeivide by zero Err Level Fatalid you forget to use a semi-colon? Err Level Annoying:Uninitialized variable in use