Perl  簡介 Sway   2010/12/28
equals(  ,  ) Cross-platform, open-source, scripting language,  multi-paradigm, dynamic typing, statement ratio: 6 There s...
Perl  特色 <ul><li>while(<>){ </li></ul><ul><li>chomp; </li></ul><ul><li>print join(“t”, (split /:/)[0, 2, 1, 5] ) , “n”; </...
CPAN <ul><li>Comprehehensive Perl Archive Network </li></ul><ul><li>http://www.cpan.org/ </li></ul><ul><li>中譯為「 Perl 綜合典藏網...
you must be joking! http://www.strombergers.com/python/ my @list = ('a', 'b', 'c');  my %hash;  $hash{‘letters'} = @list; ...
More Perl bashing… http://www.strombergers.com/python/ sub add { $_[0] + $_[1];  }   def add(a, b): return a + b sub add {...
More Perl…
  CGI Perl is widely used for web programming
Perl Syntax <ul><li>Basic Script </li></ul><ul><li>#!/usr/bin/perl </li></ul><ul><li>$inputline = <STDIN>; </li></ul><ul><...
one of ten Perl myths http://www.perl.com/pub/a/2000/01/10PerlMyths.html “… Perl works the way you do…”   “… That's one, f...
Scalars <ul><li>A scalar represents a single value:  </li></ul><ul><li>string, integer or floating point numbers.  </li></...
Arrays <ul><li>@animals = (“cat&quot;, “dog&quot;, “rabbit&quot;); </li></ul><ul><li>@nums = (11, 22, 33); </li></ul><ul><...
Hashes <ul><li>A hash is a set of key/value pairs: </li></ul><ul><li>- %ages = (“tom&quot;=>18,“jack&quot;=>21);    </li><...
Complex data types <ul><li>Reference: A scalar value </li></ul><ul><li>$scalarref = $num; </li></ul><ul><li>$arrayref = @m...
Complex data types   <ul><li>-  A reference can refer to any other Perl data type: </li></ul><ul><li>$table = { “jack”  =>...
Comparison <ul><li>Numeric  / String </li></ul><ul><li>== / eq  equal </li></ul><ul><li>!=  / ne  not equal </li></ul><ul>...
Pattern Matching <ul><li>.  any character except newline </li></ul><ul><li>s  white space (space, tab, newline...) </li></...
Conditional constructs <ul><li>if (cond ) { ... } </li></ul><ul><li>if (cond ) { ... } else { ... } </li></ul><ul><li>if (...
Conditional constructs
Looping constructs <ul><li>while (cond ) { ... } </li></ul><ul><li>while (cond ) { ... } continue { ... } </li></ul><ul><l...
Looping constructs
Subroutines <ul><li>Call subroutine  </li></ul><ul><li>-  subname();  </li></ul><ul><li>&subname; </li></ul><ul><li>-  $cu...
Perl 6 <ul><li>http://www.perl6.org/ </li></ul>
Upcoming SlideShare
Loading in...5
×

Introduction to Perl

1,498

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,498
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to Perl

  1. 1. Perl 簡介 Sway 2010/12/28
  2. 2. equals( , ) Cross-platform, open-source, scripting language, multi-paradigm, dynamic typing, statement ratio: 6 There should be one way There’s more than one way Guido van Rossum Larry Wall 1991 1987 Python Perl Easy Difficult
  3. 3. Perl 特色 <ul><li>while(<>){ </li></ul><ul><li>chomp; </li></ul><ul><li>print join(“t”, (split /:/)[0, 2, 1, 5] ) , “n”; </li></ul><ul><li>} </li></ul><ul><li>如果用 C 寫 </li></ul><ul><li>上面這段程式的大小會增長十幾倍 </li></ul>
  4. 4. CPAN <ul><li>Comprehehensive Perl Archive Network </li></ul><ul><li>http://www.cpan.org/ </li></ul><ul><li>中譯為「 Perl 綜合典藏網」 </li></ul><ul><li>「 Perl 綜合檔案網」 </li></ul><ul><li>3400+ authors </li></ul><ul><li>12000+ modules </li></ul><ul><li>CPAN shell </li></ul><ul><li>快速安裝套件 </li></ul>
  5. 5. you must be joking! http://www.strombergers.com/python/ my @list = ('a', 'b', 'c'); my %hash; $hash{‘letters'} = @list; print &quot;@{$hash{‘letters'}}n&quot;; list = ['a', 'b', 'c'] hash = {} hash[‘letters'] = list print hash[‘letters'] package Person; use strict; sub new { my $class = shift; my $age = shift or die &quot;Must pass age&quot;; my $rSelf = {'age' => $age}; bless ($rSelf, $class); return $rSelf; } class Person: def __init__(self, age): self.age = age @list = ( [‘a’, ’b’, ’c’], [1, 2, 3] ); print “@{$list[0]}n”; list = [ [‘a’, ’b’, ’c’], [1, 2, 3] ] print list[0]
  6. 6. More Perl bashing… http://www.strombergers.com/python/ sub add { $_[0] + $_[1]; } def add(a, b): return a + b sub add { my ($a, $b) = _@; return $a + $b; } sub add { my $a = shift; my $b = shift; return $a + $b; } def diff(a, b): return len(a) - len(b) sub diff { my ($aref, $bref) = _@; my (@a) = @$aref; my (@b) = @$bref; return scalar(@a) + scalar(@b); } sub add($, $) { local ($a, $b) = _@; return $a + $b; }
  7. 7. More Perl…
  8. 8.   CGI Perl is widely used for web programming
  9. 9. Perl Syntax <ul><li>Basic Script </li></ul><ul><li>#!/usr/bin/perl </li></ul><ul><li>$inputline = <STDIN>; </li></ul><ul><li>print($inputline); </li></ul><ul><li>Data Types </li></ul><ul><li>Perl has three main variable types: </li></ul><ul><li>scalars, arrays, and hashes </li></ul><ul><li>dynamically typed. </li></ul>
  10. 10. one of ten Perl myths http://www.perl.com/pub/a/2000/01/10PerlMyths.html “… Perl works the way you do…” “… That's one, fairly natural way to think about it…” while (<>) { s/(.*):(.*)/$2:$1/; print; } Swap two sections of a string: “aaa:bbb” -> “bbb:aaa” for line in file: line = line.strip() first, second = line.split(‘:’) print second+’:’+first while (<>) { chomp; ($first, $second) = split /:/; print $second, &quot;:&quot;, $first, &quot;n&quot;; } “… we can happily consign the idea that ‘Perl is hard’ to mythology.” from re import sub for line in file: print sub(‘(.*):(.*)’, r’2:1’, line)
  11. 11. Scalars <ul><li>A scalar represents a single value: </li></ul><ul><li>string, integer or floating point numbers. </li></ul><ul><li>automatic conversion. </li></ul><ul><li>$string = &quot;hello&quot;; </li></ul><ul><li>$color = ‘green’; </li></ul><ul><li>$num = 88; </li></ul><ul><li>$float = 9.58; </li></ul>
  12. 12. Arrays <ul><li>@animals = (“cat&quot;, “dog&quot;, “rabbit&quot;); </li></ul><ul><li>@nums = (11, 22, 33); </li></ul><ul><li>@mixed = (&quot;cat&quot;, 1.23); </li></ul>
  13. 13. Hashes <ul><li>A hash is a set of key/value pairs: </li></ul><ul><li>- %ages = (“tom&quot;=>18,“jack&quot;=>21); </li></ul><ul><li>- %same = (&quot;lee&quot;,18,“wang&quot;,21); </li></ul><ul><li>- %mixed = (1=>“Mon&quot;, “pi&quot;=>3.14) </li></ul><ul><li>- %empty_hash = (); </li></ul>
  14. 14. Complex data types <ul><li>Reference: A scalar value </li></ul><ul><li>$scalarref = $num; </li></ul><ul><li>$arrayref = @mixed; </li></ul><ul><li>$hashref = %ages; </li></ul>
  15. 15. Complex data types <ul><li>- A reference can refer to any other Perl data type: </li></ul><ul><li>$table = { “jack” => { age => 47; </li></ul><ul><li>id => “teacher”, </li></ul><ul><li>}, </li></ul><ul><li>“ peter” => { age => 21 </li></ul><ul><li>id => “student”, </li></ul><ul><li>}, </li></ul><ul><li>“ tom” => { age => 59 </li></ul><ul><li>id=> “retired”, </li></ul><ul><li>}, </li></ul><ul><li>}; </li></ul>
  16. 16. Comparison <ul><li>Numeric / String </li></ul><ul><li>== / eq equal </li></ul><ul><li>!= / ne not equal </li></ul><ul><li>< / lt less than </li></ul><ul><li>> / gt greater than </li></ul><ul><li><= / le less than or equal </li></ul><ul><li>>= / ge greater than or equal </li></ul>
  17. 17. Pattern Matching <ul><li>. any character except newline </li></ul><ul><li>s white space (space, tab, newline...) </li></ul><ul><li>S non-white space </li></ul><ul><li>d any digit (0-9) </li></ul><ul><li>D any non-digit </li></ul><ul><li>w any word character (a-z, A-Z, 0-9, _) </li></ul><ul><li>W any non-word character </li></ul><ul><li>[abcde] any single character within the set </li></ul><ul><li>[^abcde] any single character outside the set </li></ul><ul><li>| alternatives </li></ul><ul><li>^ beginning of string </li></ul><ul><li>$ end of string </li></ul><ul><li>* + ? { } number of occurrences </li></ul><ul><li>() pattern memory </li></ul>
  18. 18. Conditional constructs <ul><li>if (cond ) { ... } </li></ul><ul><li>if (cond ) { ... } else { ... } </li></ul><ul><li>if (cond ) { ... } elsif (cond ) { ... } else { ... } </li></ul><ul><li>Negated if statement: </li></ul><ul><li>unless cond {…} </li></ul><ul><li>post-condition </li></ul><ul><li>{…} if (cond) </li></ul>
  19. 19. Conditional constructs
  20. 20. Looping constructs <ul><li>while (cond ) { ... } </li></ul><ul><li>while (cond ) { ... } continue { ... } </li></ul><ul><li>Negated while statement </li></ul><ul><li>until (cond ) {…} </li></ul><ul><li>post-condition of while </li></ul><ul><li>{…} while (cond); </li></ul>
  21. 21. Looping constructs
  22. 22. Subroutines <ul><li>Call subroutine </li></ul><ul><li>- subname(); </li></ul><ul><li>&subname; </li></ul><ul><li>- $cub = cubic(8); </li></ul><ul><li>Example: </li></ul><ul><li>sub cubic { </li></ul><ul><li>my $num = shift; </li></ul><ul><li>my $result = $num * $num * $num; </li></ul><ul><li>return $result; </li></ul><ul><li>} </li></ul><ul><li>pass-by-value, pass-by-reference </li></ul>
  23. 23. Perl 6 <ul><li>http://www.perl6.org/ </li></ul>

×