Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 28 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
28
Dung lượng
124,5 KB
Nội dung
Perl Basics Perl Basics A Perl Tutorial NLP Course - 2006 What is Perl? What is Perl? Practical Extraction and Report Language Interpreted Language Optimized for String Manipulation and File I/O Full support for Regular Expressions Running Perl Scripts Running Perl Scripts Windows Download ActivePerl from ActiveState Just run the script from a 'Command Prompt' window UNIX – Cygwin Put the following in the first line of your script #!/usr/bin/perl Run the script % perl script_name Basic Syntax Basic Syntax Statements end with semicolon ‘;’ Comments start with ‘#’ Only single line comments Variables You don’t have to declare a variable before you access it You don't have to declare a variable's type Scalars and Identifiers Scalars and Identifiers Identifiers A variable name Case sensitive Scalar A single value (string or numerical) Accessed by prefixing an identifier with '$' Assignment with '=' $scalar = expression Strings Strings Quoting Strings With ' (apostrophe) Everything is interpreted literally With " (double quotes) Variables get expanded With ` (backtick) The text is executed as a separate process, and the output of the command is returned as the value of the string Check 01_printDate.pl String Operation Arithmetic lt less than < gt greater than > eq equal to == le less than or equal to <= ge greater than or equal to >= ne not equal to != cmp compare, return 1, 0, -1 <=> Comparison Operators Comparison Operators Operator Operation ||, or logical or &&, and logical and !, not logical not xor logical xor Logical Operators Logical Operators Operator Operation . string concatenation x string repetition .= concatenation and assignment $string1 = "potato"; $string2 = "head"; $newstring = $string1 . $string2; #"potatohead" $newerstring = $string1 x 2; #"potatopotato" $string1 .= $string2; #"potatohead" String Operators String Operators Check concat_input.pl Perl Functions Perl Functions Perl functions are identified by their unique names (print, chop, close, etc) Function arguments are supplied as a comma separated list in parenthesis. The commas are necessary The parentheses are often not Be careful! You can write some nasty and unreadable code this way! Check 02_unreadable.pl [...]... "merlyn::118:10:Randal:/home/merlyn :/usr/bin /perl" ; @fields = split(/:/,$line); # split $line, using : as delimiter # now @fields is ("merlyn","","118","10","Randal", # "/home/merlyn","/usr/bin /perl" ) Join Example $bigstring = join($glue,@list); For example to rebuilt the password file try something like: $outline = join(":", @fields); String - Pattern Examples A simple Example #!/usr/bin /perl print ("Ask me a question... shift(@numbers_10), "\n"); # Combine two ops print("Count elements (2-9): ", $#@numbers_10; # scalar( @numbers_10 ), "\n"); print("What's left (numbers 2-9): ", @numbers_10, "\n"); Hashes Example #!/usr/bin /perl # Simple List operations $player{"clarinet"} = "Susan Bartlett"; $player{"basson"} = "Andrew Vandesteeg"; $player{"flute"} = "Heidi Lawson"; $player{"oboe"} = "Jeanine Hassel"; @woodwinds = keys(%player);... all the values in the %ARRAY each(%ARRAY) iterates through the key-value pairs of the %ARRAY delete($ARRAY{KEY}) removes the key-value pair associated with {KEY} from the ARRAY Arrays Example #!/usr/bin /perl # Simple List operations # Address an element in the list @stringInstruments = ("violin","viola","cello","bass"); @brass = ("trumpet","horn","trombone","euphonium", "tuba"); $biggestInstrument = $stringInstruments[3];... ; # what about capital P in "please"? if ($question =~ /please/) { print ("Thank you for being polite!\n"); } else { print ("That was not very polite!\n"); } String – Pattern Example #!/usr/bin /perl print ("Enter a variable name:\n"); $varname = ; chop ($varname); # Try asd$asdas It gets accepted! if ($varname =~ /\$[A-Za-z][_0-9a-zA-Z]*/) { print ("$varname is a legal scalar variable\n"); . Perl Basics Perl Basics A Perl Tutorial NLP Course - 2006 What is Perl? What is Perl? Practical Extraction and Report Language Interpreted. Manipulation and File I/O Full support for Regular Expressions Running Perl Scripts Running Perl Scripts Windows Download ActivePerl from ActiveState Just run the script from a 'Command. #"potatohead" String Operators String Operators Check concat_input.pl Perl Functions Perl Functions Perl functions are identified by their unique names (print, chop, close, etc) Function