Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 55 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
55
Dung lượng
239 KB
Nội dung
Introduction to Perl programming By: Cédric Notredame (Adapted from BT McInnes) 2 What is Perl? Perl is a Portable Scripting Language No compiling is needed. Runs on Windows, UNIX, LINUX and cygwin Fast and easy text processing capability Fast and easy file handling capability Written by Larry Wall “Perl is the language for getting your job done.” Too Slow For Number Crunching Ideal for Prototyping 3 How to Access Perl To install at home Perl Comes by Default on Linux, Cygwin, MacOSX www.perl.com Has rpm's for Linux www.activestate.com Has binaries for Windows Latest Version is 5.8 To check if Perl is working and the version number % perl -v 4 Resources For Perl Books: Learning Perl By Larry Wall Published by O'Reilly Programming Perl By Larry Wall,Tom Christiansen and Jon Orwant Published by O'Reilly Web Site http://safari.oreilly.com Contains both Learning Perl and Programming Perl in ebook form 5 Web Sources for Perl Web www.perl.com www.perldoc.com www.perl.org www.perlmonks.org 6 The Basic Hello World Program which perl pico hello.pl Program: #! /…path…/perl -w print “Hello World!\n”; Save this as “hello.pl” Give it executable permissions chmod a+x hello.pl Run it as follows: ./hello.pl 7 “Hello World” Observations “.pl” extension is optional but is commonly used The first line “#!/usr/local/bin/perl” tells UNIX where to find Perl “-w” switches on warning : not required but a really good idea Variables and Their Content 9 Numerical Literals Numerical Literals 6 Integer 12.6 Floating Point 1e10 Scientific Notation 6.4E-33 Scientific Notation 4_348_348 Underscores instead of commas for long numbers 10 String Literals String Literals “There is more than one way to do it!” 'Just don't create a file called -rf.' “Beauty?\nWhat's that?\n” “” “Real programmers can write assembly in any language.” Quotes from Larry Wall [...]... looks for variables inside strings and replaces them with their value $stooge = “Larry” print “$stooge is one of the three stooges.\n”; Produces the output: Larry is one of the three stooges This does not happen when you use single quotes print '$stooge is one of the three stooges.\n’; Produces the output: $stooge is one of the three stooges.\n 18 Character Interpolation List of character escapes that... Functions Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case $name = lc($name); Convert only the first char to lower case $name = lcfirst($name); 15 A String Example Program Convert to upper case $name = uc($name); Convert only the first char to upper case $name = ucfirst($name); Convert to lower case... Equality Operators for Strings Equality/ Inequality : eq and ne $language = Perl ; if ($language == Perl ) # Wrong! if ($language eq Perl ) #Correct Use eq / ne rather than == / != for strings 13 Relational Operators for Strings Greater than Numeric : > String : gt Greater than or equal to Numeric : >= String : ge Less than Numeric : < String : lt Less than or equal to Numeric... STDERR Variables do not need to be declared Variable type (int, char, ) is decided at run time $a = 5; # now an integer $a = perl ; # now a string 11 Operators on Scalar Variables Numeric and Logic Operators Typical : +, -, *, /, %, ++, , +=, -=, *=, /=, ||, &&, ! ect … Not typical: ** for exponentiation String Operators Concatenation: “.” - similar to strcat $first_name = “Larry”;... /usr/bin /perl for ($i=0, $j=0; $i . Learning Perl and Programming Perl in ebook form 5 Web Sources for Perl Web www .perl. com www.perldoc.com www .perl. org www.perlmonks.org 6 The Basic Hello World Program which perl pico. capability Written by Larry Wall Perl is the language for getting your job done.” Too Slow For Number Crunching Ideal for Prototyping 3 How to Access Perl To install at home Perl Comes by Default. MacOSX www .perl. com Has rpm's for Linux www.activestate.com Has binaries for Windows Latest Version is 5.8 To check if Perl is working and the version number % perl -v 4 Resources For Perl Books: Learning