Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 135 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
135
Dung lượng
719,5 KB
Nội dung
1 Introduction to Perl 2 What is Perl? • Practical Extraction and Report Language • A scripting language which is both relatively simple to learn and yet remarkably powerful. 3 Introduction to Perl Perl is often described as a cross between shell programming and the C programming language. C (numbers) Shell programming (text) Smalltalk (objects) C++ (numbers, objects) Perl (text, numbers) Java (objects) 4 Introduction to Perl • A “glue” language. Ideal for connecting things together, such as a GUI to a number cruncher, or a database to a web server. • Has replaced shell programming as the most popular programming language for text processing and Unix system administration. • Runs under all operating systems (including Windows). • Open source, many libraries available (e.g. database, internet) • Extremely popular for CGI and GUI programming. 5 Why use Perl ? • It is easy to gain a basic understanding of the language and start writing useful programs quickly. • There are a number of shortcuts which make programming ‘easier’. • Perl is popular and widely used, especially for system administration and WWW programming. 6 Why use Perl? • Perl is free and available on all computing platforms. – Unix/Linux, Windows, Macintosh, Palm OS • There are many freely available additions to Perl (‘Modules’). • Most importantly, Perl is designed to understand and manipulate text. 7 Where to find help! • http://www.perl.com • http://www.perl.org 8 Your first Perl script #!/usr/bin/perl #This script prints a friendly greeting to the screen print “Hello World\n”; • Scripts are first “compiled” and then “executed” in the order in which the lines of code appear • You can write a script with any text editor. The only rule is that it must be saved as plain text. 9 Running Perl Scripts • Perl 5 is installed on our CS system. • Run from the command line: palazzi% which perl /usr/bin/perl palazzi$ perl hello.pl Hello world! • You can run the script directly if you make the script executable, and the first line uses ‘hash-bang’ notation: palazzi% chmod +x hello.pl palazzi% hello.pl #!/usr/bin/perl -w print "Hello world!\n"; 10 Basic Syntax • The -w option tells Perl to produce extra warning messages about potential dangers. Always use this option- there is never (ok, rarely) a good reason not to. #!/usr/bin/perl -w • White space doesn't matter in Perl (like C++), except for #!/usr/bin/perl -w which must start from column 1 on line 1. [...]... # welcome.pl print ( "1 Welcome to Perl! \n" ); print "2 Welcome to Perl! \n" ; print "3 Welcome ", "to ", "Perl! \n"; print "4 Welcome "; print "to Perl! \n"; print "5 Welcome to Perl! \n"; print "6 Welcome\n to\ n\n Perl! \n"; 1 Welcome to Perl! 2 Welcome to Perl! 3 Welcome to Perl! 4 Welcome to Perl! 5 Welcome to Perl! 6 Welcome to Perl! 15 System Calls • You can use Perl to execute shell commands, just... string\n^; • print qq &Or this one\n&; – perl thinks that if you use a ‘(‘, ‘[‘, or ‘{‘ to open the string, you mean to use a ‘)’, ‘]’, or ‘}’ to close it 31 What is Truth? • A question debated by man since before cave art • A very defined thing in PERL – Something is FALSE if: • • • • a) it evaluates to zero b) it evaluates to ‘’ (empty string) c) it evaluates to an empty list (@array = “”) d) the value...Basic Syntax • All Perl statements end in a semicolon ; (like C) • In Perl, comments begin with # (like shell scripts) – everything after the # to the end of the line is ignored – # need not be at the beginning of the line – there are no C-like multi-line comments: /* */ 11 Perl Example • Back to our “Hello World” program: palazzi% hello.pl #!/usr/bin /perl -w # This is a simple Hello... - false d) (1+56) < = Description Equality Non Equality Greater Than Less Than Greater Than or Equal Less Than or Equal Comparison " " Example 2 == 2 2 !=2 3>2 3 . complete. 15 Many ways to do it! 1. Welcome to Perl! 2. Welcome to Perl! 3. Welcome to Perl! 4. Welcome to Perl! 5. Welcome to Perl! 6. Welcome to Perl! # welcome.pl print ( "1. Welcome to Perl!
". Welcome to Perl!
" ; print "3. Welcome ", " ;to ", " ;Perl!
"; print "4. Welcome "; print " ;to Perl!
"; print "5. Welcome to Perl!
"; print. 1 Introduction to Perl 2 What is Perl? • Practical Extraction and Report Language • A scripting language which is both relatively simple to learn and yet remarkably powerful. 3 Introduction to Perl Perl