Sharing

2013年5月1日 星期三

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

Chapter 4 Subroutines

@_    =>  Parameter array, use $_[0], $_[1].... $[n] to access it.

Persistent, Private Variables


Sample of persistent scalar

use 5.010;
sub marine {
    state $n = 0; # private, persistent variable $n
    $n += 1;
    print "Hello, sailor number $n!\n";
}

Sample of persistent array/list

use v5.10;

running_sum( 5, 6 );
running_sum( 1..3 );
running_sum( 4 );

sub running_sum {
    state $sum = 0;
    state @numbers;
    foreach my $number ( @_ ) {
        push @numbers, $number;
        $sum += $number;
    }
    say "The sum of (@numbers) is $sum";
}

There’s a slight restriction on arrays and hashes as state variables, though. You can’t initialize them in list contexts

state @array = qw(a b c); # Error!

Chapter 5 Input and Output


Input from Standard Input

兩種寫法都可以, 第二種是第一種的 Shortcut
while (defined($_ = )) {
    print "I saw $_";
}

while () {
    print "I saw $_";
}

# not recommended, it will read all of the input before the loop can start running
foreach () {
    print "I saw $_";
}


Input from the Diamond Operator


Diamond Operator read invocation arguments as input files and merge them together.
If you give no invocation arguments, the program should process the standard input stream.

第二種是第一種的縮寫
while (defined($line = <>)) {
    chomp($line);
    print "It was $line that I saw!\n";
}

while (<>) {
    chomp;
    print "It was $_ that I saw!\n";
}



Special filehandle


STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUT


open BEDROCK, '>:encoding(UTF-8)', $file_name;
open BEDROCK, '>:utf8', $file_name;
open BEDROCK, '>:encoding(iso-8859-1)', $file_name;
open BEDROCK, '>:crlf', $file_name;
binmode STDOUT; # don't translate line endings
binmode STDOUT, ':encoding(UTF-8)';


Fatal Errors with die


if ( ! open LOG, '>>', 'logfile' ) {
    die "Cannot create logfile: $!";
}

Using Filehandles


You can use a filehandle open for writing or appending with print or printf.
There is no comma between the filehandle and the items to be printed.

print LOG "Captain's log, stardate 3.14159\n"; # output goes to LOG
printf STDERR "%d percent complete.\n", $done/$total * 100;

Changing the Default Output Filehandle

Default output may be changed with the select operator
select BEDROCK;
print "I hope Mr. Slate doesn't find out about this.\n";
print "Wilma!\n";

Setting the special $| variable to 1 will set the currently selected file handle to always flush the buffer after each output operation.
select LOG;
$| = 1; # don't keep LOG entries sitting in the buffer
select STDOUT;
# ... time passes, babies learn to walk, tectonic plates shift, and then...
print LOG "This gets written to the LOG at once!\n";

Chapter 6 Hashes

The keys and values are both arbitrary scalars, but the keys are always converted to strings.

Hash Assignment


Now the keys are where the values used to be, and the values are where the keys used to be
my %inverse_hash = reverse %any_hash;

The keys and values Functions

my %hash = ('a' => 1, 'b' => 2, 'c' => 3);
my @k = keys %hash;
my @v = values %hash;

The each Function

while ( ($key, $value) = each %hash ) {
    print "$key => $value\n";
}

The exists Function


if (exists $books{"dino"}) {
    print "Hey, there's a library card for dino!\n";
}


沒有留言: