Sharing

2013年4月30日 星期二

Perl 學習手冊第六版筆記(一)

Chapter 2 Scalar Data

Single-Quoted String Literals

  • 不轉換 \n\t等空格字元
  • 不轉換 $var 變數
  • 支持多行輸入

Double-Quoted String Literals

  • 支持 backslash escapes
  • 支持 variable interpolated
  • 不支持多行輸入, 用 "\n" 來控制斷行

\n Newline
\r Return
\t Tab
\f Formfeed
\b Backspace
\a Bell
\e Escape(ASCII escape character)
\007 Any octal ASCII value (here, 007= bell)
\x7f Any hex ASCII value (here, 7f= delete)
\x{2744} Any hex Unicode code point (here, U+2744= snowflake)
\cC A “control” character (here, Ctrl-C)
\\ Backslash
\” Double quote
\l Lowercase next letter
\L Lowercase all following letters until \E
\u Uppercase next letter
\U Uppercase all following letters until \E
\Q Quote nonword characters by adding a backslash until \E
\E End \L, \U, or \Q


Comparison Operators

Comparison Numeric String
Equal == eq
Not equal != ne
Less than < lt
Greater than > gt
Less than or equal to <= le
Greater than or equal to >= ge

Boolean Values

Because the string '0' is the exact same scalar, value as the number 0, Perl has to treat them both the same. That means that the string '0' is the only non-empty string that is false.

以下這段程式碼會印 false
if ("0"){
    print 'true'
} else {
    print 'false'
}

$still_true = !! 'Fred';    # return True Boolean value
$still_false = !! '0';      # return False Boolean value


Chapter 3 Lists and Arrays


The qw Shortcut

qw stands for “quoted words” or “quoted by white space”. Either way, Perl treats it like a single-quoted string. Perl actually lets you choose any punctuation character as the delimiter.

qw( fred barney betty wilma dino )
qw! fred barney betty wilma dino !
qw/ fred barney betty wilma dino /

The splice Operator

# second argument: start point
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 2; # remove everything after fred
                             # @removed is qw(fred barney betty)
                            # @array is qw(pebbles dino)

# third argument: length
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 2; # remove dino, fred
                                # @removed is qw(dino fred)
                                # @array is qw(pebbles barney betty)

# fourth argument: replacement list
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 2, qw(wilma); # remove dino, fred
                                           # @removed is qw(dino fred)
                                           # @array is qw(pebbles wilma barney betty)
                                           
                                           
# special case for length = 0, insert the list into original array 
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 0, qw(wilma); # remove nothing
                                           # @removed is qw()
                                           # @array is qw(pebbles wilma dino fred barney betty

The each Operator

Every time that you call each on an array, it returns two values for the next element in
the array—the index of the value and the value itself

@rocks = qw/ bedrock slate rubble granite /;
while( my( $index, $value ) = each @rocks ) {
    say "$index: $value";
}

Scalar Context

$fred = something;
$fred[3] = something;
123 + something
something + 654
if (something) { ... }
while (something) { ... }
$fred[something] = something;

List Context

@fred = something;
($fred, $barney) = something;
($fred) = something;
push @fred, something;
foreach $fred (something) { ... }
sort something
reverse something
print something

Forcing Scalar Context

@rocks = qw( talc quartz jade obsidian );
print "How many rocks do you have?\n";
print "I have ", @rocks, " rocks!\n";
# WRONG, prints names of rocks
print "I have ", scalar @rocks, " rocks!\n"; # Correct, gives a number


@food  = ("apples", "pears", "eels");
print @food,"\n";             # By itself, print out "applespearseels"
print "@food","\n";           # Embedded in double quotes, print out "apples pears eels"
print @food."\n";             # In a scalar context, print out "3"

my($num) = @_;    # list context, same as ($num) = @_, get the first paramter
my $num = @_;     # scalar context, same as $num = @_, get the number of paramters

沒有留言: