Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 35 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
35
Dung lượng
440 KB
Nội dung
Chapter 11: Perl Scripting Off Larry’s Wall In this chapter … • • • • • • • Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions Perl • Practical Extraction and Report Language • Developed by Larry Wall in 1987 • Originally created for data processing and report generation • Elements of C, AWK, sed, scripting • Add-on modules and third party code make it a more general programming language Features • • • • • C-derived syntax Ambiguous variables & dynamic typing Singular and plural variables Informal, easy to use Many paradigms – procedural, functional, object-oriented • Extensive third party modules Features, con’t • • • • • As elegant as you make it Do What I Mean intelligence Fast, easy, down and dirty coding Interpreted, not compiled perldoc – man pages for Perl modules Terminology • Module – one stand alone piece of code • Distribution – set of modules • Package – a namespace for one or more distributions • Package variable – declared in package, accessible between modules • Lexical variable – local variable (scope) Terminology, con’t • Scalar – variable that contains only one value (number, string, etc) • Composite – variable made of one or more scalars • List – series of one or more scalars – e.g (2, 4, ‘Zach’) • Array – composite variable containing a list Invoking Perl • perl –e ‘text of perl program’ • perl perl_script • Make perl script executable and you can execute the script itself – i.e ./my_script.pl • Common file extension pl not required • Like other scripts start with #! to specify execution program Invoking Perl, con’t • Use perl –w to display warnings – Will warn if using undeclared variables – Instead of –w, use warnings; in your script • Same effect • Usually you’ll find perl in /usr/bin/perl Syntax • Each perl statement ended by semicolon (;) • Can have multiple statements per line • Whitespace ignored largely – Except within quoted strings • Double quotes allow interpretation of variables and special characters (like \n) • Single quotes don’t (just like the shell) Hashes • • • • • • • Plural, contain an array of key-value pairs Prefix with % i.e %myhash Keys are strings, act as indexes to array Each key must be unique, returns one value Unordered Optimized from random access Keys don’t need quotes unless there are spaces Hashes, con’t • Element access – $hashvar{index} = value • e.g $myvar{boat} =“tuna”; print $myvar{boat}; – %hashvar = ( key => value, …); • e.g %myvar = ( boat => “tuna”, => “fish”); – Get array of keys or values • keys(%hashvar) • values(%hashvar) Evaluating Expressions • Most control structures use an expression to evaluate whether they are run • Perl uses different comparison operators for strings and numbers • Also uses the same file operators (existence, access, etc) that bash uses Expressions • Numeric operators – ==, !=, , = – returns if equal, if >, -1 if < • String Operators – eq, ne, lt, gt, le, ge – cmp same as Control Structures • • • • • • if (expr) {…} unless (expr) {…} if (expr) {…} else {…} if (expr) {…} elsif (expr) {…} … else {…} while (expr) {…} until (expr) {…} Control Structures, con’t • for and foreach are interchangeble • Syntax – Similar to bash for…in structure – foreach [var] (list) {…} – If var not defined, $_ assumed – For each loop iteration, the next value from list is populated in var Control Structures, con’t • for/foreach Syntax – Similar to C’s for loop – foreach (expr1; expr2; expr3) {…} – expr1 sets initial condition – expr2 is the terminal condition – expr3 is the incrementor Control Structures, con’t • Short-circuiting loops – Use last to break out of loop altogether • Same as bash’s break – Use next to skip to the next iteration of the loop • Same as bash’s continue Handles • A handle is essentially a variable linked to a file or process • Perl automatically opens handles for the default streams – STDIN, STDOUT, STDERR • You can open additional handles – To a file for input/output/appending – To a process for input/output Handles, con’t • Basic syntax – open(handle, [‘mode’], “ref”); – handle is a variable to reference the handle – mode can be many things • Simple cases: , >>, | • Input (>/var/log/errors”); open(my $SORT, “| sort –n”); open(my $ALIST, "grep \'^[Aa]\' /usr/share/dict/words|") while() { print $ERRLOG $_; } Regular Expressions • Recall Appendix A • Perl has a few unique features and caveats • Regular Expressions (RE) delimited by forward slash • Perl uses the =~ operator for RE matching – Ex if ($myvar =~ /^T/) { …} # if myvar starts w/ T • To negate RE matching use !~ operator RE, con’t • =~ operator can also be used to replacement – Ex $result =~s/old/new/; – ‘old’ replaced with ‘new’ if matched • Remember, RE (esp in Perl) are greedy – Will match longest possible match • Bracketed expressions don’t need to be escaped, just use parentheses ... Syntax – Similar to C’s for loop – foreach (expr1; expr2; expr3) {…} – expr1 sets initial condition – expr2 is the terminal condition – expr3 is the incrementor Control Structures, con’t • Short-circuiting...In this chapter … • • • • • • • Background Terminology Syntax Variables Control Structures File Manipulation