Perl  簡介 Sway   2010/12/28
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
Perl  特色 while(<>){ chomp; print join(“\t”, (split /:/)[0, 2, 1, 5] ) , “\n”; } 如果用 C 寫 上面這段程式的大小會增長十幾倍
CPAN Comprehehensive Perl Archive Network http://www.cpan.org/ 中譯為「 Perl 綜合典藏網」 「 Perl 綜合檔案網」 3400+ authors 12000+ modules CPAN shell 快速安裝套件
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]
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;  }
More Perl…
  CGI Perl is widely used for web programming
Perl Syntax Basic Script #!/usr/bin/perl $inputline = <STDIN>; print($inputline);  Data Types Perl has three main variable types: scalars, arrays, and hashes dynamically typed.
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)
Scalars A scalar represents a single value:  string, integer or floating point numbers.  automatic conversion. $string = &quot;hello&quot;; $color = ‘green’; $num = 88; $float = 9.58;
Arrays @animals = (“cat&quot;, “dog&quot;, “rabbit&quot;); @nums = (11, 22, 33); @mixed  = (&quot;cat&quot;, 1.23);
Hashes A hash is a set of key/value pairs: - %ages = (“tom&quot;=>18,“jack&quot;=>21);    - %same = (&quot;lee&quot;,18,“wang&quot;,21); - %mixed = (1=>“Mon&quot;, “pi&quot;=>3.14) - %empty_hash = ();
Complex data types Reference: A scalar value $scalarref = \$num; $arrayref = \@mixed; $hashref = \%ages;
Complex data types   -  A reference can refer to any other Perl data type: $table = { “jack”  =>  { age => 47; id => “teacher”, }, “ peter” =>  { age => 21 id => “student”, }, “ tom” =>  { age => 59 id=> “retired”, }, };
Comparison Numeric  / String == / eq  equal !=  / ne  not equal <  /  lt  less than   >  / gt  greater than  <=  /  le  less than  or equal  >=  /  ge  greater than  or equal
Pattern Matching .  any character except newline \s  white space (space, tab, newline...) \S  non-white space  \d  any digit (0-9) \D  any non-digit \w  any word character (a-z, A-Z, 0-9, _) \W  any non-word character [abcde]  any single character within the set [^abcde]  any single character outside the set |  alternatives  ^  beginning of string $  end of string *  + ? { }  number of occurrences ()  pattern memory
Conditional constructs if (cond ) { ... } if (cond ) { ... } else { ... } if (cond ) { ... } elsif (cond ) { ... } else { ... } Negated if statement: unless cond {…} post-condition   {…} if (cond)
Conditional constructs
Looping constructs while (cond ) { ... } while (cond ) { ... } continue { ... } Negated while statement until (cond ) {…} post-condition of while {…} while (cond);
Looping constructs
Subroutines Call subroutine  -  subname();  &subname; -  $cub = cubic(8);   Example:  sub cubic { my $num = shift; my $result = $num * $num * $num; return $result; } pass-by-value, pass-by-reference
Perl 6 http://www.perl6.org/

Introduction to Perl

  • 1.
    Perl 簡介Sway 2010/12/28
  • 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.
    Perl 特色while(<>){ chomp; print join(“\t”, (split /:/)[0, 2, 1, 5] ) , “\n”; } 如果用 C 寫 上面這段程式的大小會增長十幾倍
  • 4.
    CPAN Comprehehensive PerlArchive Network http://www.cpan.org/ 中譯為「 Perl 綜合典藏網」 「 Perl 綜合檔案網」 3400+ authors 12000+ modules CPAN shell 快速安裝套件
  • 5.
    you must bejoking! 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.
    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.
  • 8.
      CGI Perlis widely used for web programming
  • 9.
    Perl Syntax BasicScript #!/usr/bin/perl $inputline = <STDIN>; print($inputline); Data Types Perl has three main variable types: scalars, arrays, and hashes dynamically typed.
  • 10.
    one of tenPerl 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.
    Scalars A scalarrepresents a single value: string, integer or floating point numbers. automatic conversion. $string = &quot;hello&quot;; $color = ‘green’; $num = 88; $float = 9.58;
  • 12.
    Arrays @animals =(“cat&quot;, “dog&quot;, “rabbit&quot;); @nums = (11, 22, 33); @mixed = (&quot;cat&quot;, 1.23);
  • 13.
    Hashes A hashis a set of key/value pairs: - %ages = (“tom&quot;=>18,“jack&quot;=>21); - %same = (&quot;lee&quot;,18,“wang&quot;,21); - %mixed = (1=>“Mon&quot;, “pi&quot;=>3.14) - %empty_hash = ();
  • 14.
    Complex data typesReference: A scalar value $scalarref = \$num; $arrayref = \@mixed; $hashref = \%ages;
  • 15.
    Complex data types - A reference can refer to any other Perl data type: $table = { “jack” => { age => 47; id => “teacher”, }, “ peter” => { age => 21 id => “student”, }, “ tom” => { age => 59 id=> “retired”, }, };
  • 16.
    Comparison Numeric / String == / eq equal != / ne not equal < / lt less than > / gt greater than <= / le less than or equal >= / ge greater than or equal
  • 17.
    Pattern Matching . any character except newline \s white space (space, tab, newline...) \S non-white space \d any digit (0-9) \D any non-digit \w any word character (a-z, A-Z, 0-9, _) \W any non-word character [abcde] any single character within the set [^abcde] any single character outside the set | alternatives ^ beginning of string $ end of string * + ? { } number of occurrences () pattern memory
  • 18.
    Conditional constructs if(cond ) { ... } if (cond ) { ... } else { ... } if (cond ) { ... } elsif (cond ) { ... } else { ... } Negated if statement: unless cond {…} post-condition {…} if (cond)
  • 19.
  • 20.
    Looping constructs while(cond ) { ... } while (cond ) { ... } continue { ... } Negated while statement until (cond ) {…} post-condition of while {…} while (cond);
  • 21.
  • 22.
    Subroutines Call subroutine - subname(); &subname; - $cub = cubic(8); Example: sub cubic { my $num = shift; my $result = $num * $num * $num; return $result; } pass-by-value, pass-by-reference
  • 23.