Perl w1
- a little thought on Perl
- make sure you know the context when writing perl program
- make sure you understand which type of var you are using
- scalar always start with
$ - list always start with
@ - dict always start with
%
- scalar always start with
I/O
- input from stdin, one line at a time:
while (<STDIN>) {…}, this is a shorthand forwhile (defined(<STDIN>) ) - input from a handler, store each line as one element in a array,
my @ar = <file> - input from command-line argument.
while (<>) {…}e.g../read.pl file1 file2 …will get input from file1, then file2, then… - special symbol: all arguments from command-line stores in
@ARGV, a special array. - get the output from external unix command:
open my $file, "ls -al |"open a pipe - send the handler to external unix command:
open $file, "| lpr" - Idiom
- read file word by word
- read entire file as string
use File::Slurp; - read entire file as string
my $file = read_file($ARGV[0]) - split the string into words
my @words = split /[ \t\r\n]+/, $file - iterate thru
for (@words) …
- read entire file as string
- read file char by char
- read entire file as string use
File::Slurp - split the string into characters
my @chars = split //, $file - iterate thru
for (@chars) … - more efficient you can split char line by line
while <$file> {@chars = split //}
- read entire file as string use
- read file word by word
File handles
-
Filehandler is actually a pointer
-
three default file handlers:
STDIN STDOUT STDERR - the
opentells Perl to ask os to open connection between your program and the outside world opencan open any connection to real world, e.g. socket, process, file, pipe…- meaning, if you want to input from other text file rather than
STDIN, useopen HANDLERNAME, "<", "textfile"first, then treat HANDLERNAME just as normal stdin. - meaning, if you want to output to other text file rancher than
STDOUT, useopen HANDLER, ">", "textfile"first, then treat HANDLER just as STDOUT. - e.g.
open IN, "<", "textfile"; for (@IN) {…}read from file - e.g.
open OUT, ">", "textfile"; select OUT; print…;write to file - e.g.
open OUT, ">", "textfile"; print OUT, "…";write to file just use print openwill return true if successfully opened, otherwise false
- meaning, if you want to input from other text file rather than
- special symbol:
$!print the essential information about system error. e.g.print "bad, $!"- Bad FileHandle:
my $ok = open LOG, ">>", "log.txt"; if(!ok) {…}
- Bad FileHandle:
Subroutine
-
invoke:
&name(parameter1, parameter2,…): e.g.$n = &max(1,2) -
mutable parameters, all vars get passed into sub are reference, meaning all mutable !
- special symbol:
@_, all parameters passed into subroutine stored in special array@_, you can retrieve any value like normal. e.g.$_[0]- important: these
$_[2]values has nothing to do with$_, don’t get confused. - give name to default parameters:
($name1, $name2) = ($_[0], $_[1])or($name1, $name2, $name3…) = @_
- important: these
- return value: last expression evaluated will be return value!
- the
returnkeyword isn’t really necessary, for simplicity, you can omit, because always the last expression gets evaluated return its value
- the
- prototype:
sub add($$)tell perl this sub need exactly two scalar.sub divide($@)tell perl this sub need exactly one scalar and one array
Hash
- a little thought on Perl
- make sure you know the context when writing perl program
- make sure you understand which type of var you are using
- scalar always start with
$ - list always start with
@ - dict always start with
%
- scalar always start with
- what is dict
- key-value pair.
- key: must be string.
- value: must be scalar var.
- random access, no order guarantee !
- dict assignment
my %dict; $dict{"s344"} = "alex";my %dict = qw(s344 alex s345 pcy);my %dict = ("s344", 2, "s355", 1);my %dict = ("s344"=>2, "s444"=>1);- the
%dictaccepts a list, and convert elements to key-value pair, the example above may be converted like “(key,value,key,value)”
- dict as a whole
my %dict; %rev = reverse %dict;will get “(value=>key)” pairs@list = %dict; print "@list"will print a “(key, value)” list
- dict operation
keys: yield a list of all keys in hash.my @k = keys %dict;values: yield a list of all values in hash.my @v = values %dict;- iterate thru
each: return next key-value pair. ` while( my($k, $v) = each %hash ) {…}` more efficientfor (keys %dict) { my $v = $dict{$_} …}less efficient- you should not delete a key out of a hash (or add keys) while iterating over it using each(), because this may confuse Perl: instead, creating a new flittered array or hash with push() or dict assignment while iterated thru SRC
- delete key
- delete:
delete $books{$person}delete key-value pair, tricky is the return value is that key!
- delete:
- tips:
my $count = keys %dict;return # of keys ! - tips:
print %dictwill print content of hash in an array.