Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Why should I learn/use Perl? • Perl is fun • Perl is useful for… • Text processing • Web programming • System administration • Game programming • Scientific research • bio-informatics, linguistics • Open Source
How to ‘get’ Perl – Windows • Strawberry Perl • For Windows only • Just the same as Perl elsewhere • http://strawberryperl.com/ • Recommended!! • ActivePerl • For Windows, Linux, Mac OS X, Solaris, AIX and HP-UX • http://www.activestate.com/activeperl/
Scalars • Numbers • Decimal floating point • (Can be made integer, octal, hexadecimal) • Strings • Can contain any character • Can be null: “” • Can be undef
Strings • Single-quoted • characters are as shown with only two exceptions. • single-quote in a single-quoted string requires \’ • backslash in a single-quoted string requires \\ • Double-quoted • it will interpolate– calculate variables or control sequences. • For example • $foo = “myfile”; • $datafile = “$foo.txt”; • will result in the variable $datafile holding the string “myfile.txt” • Another example • print ‘Howdy\n’; will print: • Howdy\n • print “Howdy\n”; will print • Howdy • (\n is a control sequence, standing for “new line”).
Scalar operators • Math, just like C/C++ • *, /, %, **, etc. • Strings • x to repeat the thing on the left • “hi” x 5 gives “hihihihihi” • . concatenates strings • “Hello” . “ World!” • Perl knows to convert when mixing these two types: • “3”*7 gives 21 • “3”.7 gives “37”
Comparing Scalars Comparison Numeric String • Equal == eq • Not equal != ne • Less than < lt • Greater than > gt • Less or equal <= le • Greater or equal >= ge 8 < 25 TRUE! “8” lt “25” FALSE!
Variables • A sign, followed by a letter, followed by pretty much whatever. • Sign determines the type: • $foo is a scalar • @foo is a array hold a list of scalars • %foo is a hash • Variables default to global (they apply in all parts of your program). This can be problematic. • local $var will make the variable active only for the current “block” of code. • my $var does the same, and dies when out the current “block”
Using Arrays • Elements are indexed, from 0. • my@animals = (“frog”, “bear”, “elephant”); • $animals[2]; # elephant • Note: element is a scalar, so $ rather than @ • Subsections are “slices”. • my@mammals = @animals[1,2]; • Lots of functions for • push, pop … • splitting a scalar string into an array • my$sentence = “元智大學 資訊工程系”; • my@words = split(“ “, $sentence); # @words contains (“元智大學”, “資訊工程系”);
Control flow • Control structures (generally like C/C++) • if / then / elsif / else • while • do {} while • do {} until • for () / foreach() # loops over a list • switch (NOTE: After perl 5.10) • … • Errors / warnings • die“message” terminates program and output “message”. • warn“message” give you a warning and keeps going.
Hashes • “Associative arrays” • A set of • values (any scalar), indexed by • keys (strings) • Example • my%info; • $info{ “name” } = “宋政隆”; • $info{ “age” } = 32; • $info{ “future” } = undef; • hashes 與 arrays 可以摻在一起用 • arrays of arrays, arrays of hashes, hashes of arrays …
Matching string patterns using regular expressions • m/pattern/ will check the last stored variable ($_) for pattern. • $var =~ m/pattern/; will check $var for pattern. • If the pattern is in $var, then • $var =~ m/pattern/ is TRUE. • If you “group” part of the pattern and it is present, • $var =~ m/(pattern)/ is true, AND, now a variable names $1 contains the first match it found. • Group more pieces of the pattern and the matches are stored in $2, $3, etc. • This only grabs the *first* match. To grab all, say • my @matches = ($var =~ m/(pattern)/g); • This will store every match in the array @matches.
What’s a “regular expression”? . any single character * zero or more of the previous + one or more of the previous ? zero or one of the previous {n} match exact n times {n,} match at least n times {n,m} match at least n but not more than m times [] character class ^ beginning of the line $ end of the line \b word boundary \d \D digit / non-digit \s \S space / non-space \w \W word character / non-word character | or – match this or that () grouping
Google Page Rank via WWW::Google::PageRank use WWW::Google::PageRank; my$url = shift; my$gpr = WWW::Google::PageRank->new(); printscalar($gpr->get ($url)); % perl mpr.pl www.cse.yzu.edu.tw 6
What is CPAN • Since 1995-10-26 • Collection of Perl software/class/documentation • DRY (Don’t Repeat Yourself) • 8000+ authors • ~18000 modules • Testers report for ALL module