Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 64 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
64
Dung lượng
536,5 KB
Nội dung
Perl course ! !!"##$ %&&% Books The beginner book Learning Perl'( )*+!, )/ The bible Programming Perl'0 1'!+2-, )/ !34 Links 5 66 6 ! 66 6 !6 66 6 - 66 6636 Perl strengths and weaknesses )- 738 )98'5! 8 1 :;! <== -> >! ,/!! Variables ;,/? ; ! ! ! ! ! ; @ !'A ! A ; ,!! /' ?3! B8!?;"'?'?5'?!!C Numbers and operators >!==!D $num = 1; $num = 324.657; $num = -0.043; ! E3F6FFG H,/+,/I,8/J,/KK,/LL ,/ !! EE33 Strings $string = ’This is a literal string’; $string = ”This is an $interpolated string\n”; M ; ! !' N N M! 8 ;; .,/x,/ Conditional statement ! if ( predicate ) { # this will be executed if the predicate is true } !8;! if ( predicate ) { # this will be executed if the predicate is true } elsif ( predicate2 ) { # no spelling mistake # this will be executed if this predicate is true } else { # finally this is excuted if no predicates where true } unless ( predicate ) { # this will be executed if the predicate is false } Predicates !8 ;!!8 >!! < > <= >= == != <=> ! lt gt le ge eq ne cmp && and || or ! not xor B8! $age > 18 and $height < 1.4 ($name eq ’Peter’ or $name eq ’Chris’) and $wage <= 25000 3,*/; Loops - while # some initialization while ( predicate ) { # code which is executed while the predicate is true } ;! until ( predicate ) { # code which is executed while the predicate is false } do { # code } while ( predicate ); [...]... too if (defined $line) {} A simple Perl program #!/usr/bin /perl –w print ”Hello user !\nWhat is your name: ”; $name = ; chomp $name; if ($name eq ’Peter’) { print ”Ahh, welcome back, sensei\n”; } else { print ”Good of you to come, $name\n”; } Strict Perl Perl is a rather loose and forgiving language This can be improved somewhat by using strict #!/usr/bin /perl –w use strict; This will enforce... keyboard like any lines are read from a filehandle $line = ; Perl is mostly used in applications where linebased I/O makes sense, even though Perl can do other types of I/O When reading a line, it is important to realize that a line ends with a newline, which is part of what is read You have to get rid of that newline so often that Perl has a function for that: chomp $line; If there is no input... in $filename is piped (read) to Perl open(IN, ’ 80; $x = 0 unless $x > 0; print ”$i\n” for ($i = 1; $i . %&&% Books The beginner book Learning Perl& apos;( )*+!, )/ The bible Programming Perl& apos;0 1'!+2-,. value. ' if (defined $line) {} A simple Perl program #!/usr/bin /perl –w print ”Hello user !
What is your name: ”; $name = <STDIN>; chomp $name; if. of you to come, $name
”; } Strict Perl ; !;! #!/usr/bin /perl –w use strict;