Your SlideShare is downloading. ×
0
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Introduction to Perl
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Introduction to Perl

1,486

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,486
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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 Perl Archive Network
    • http://www.cpan.org/
    • 中譯為「 Perl 綜合典藏網」
    • 「 Perl 綜合檔案網」
    • 3400+ authors
    • 12000+ modules
    • CPAN shell
    • 快速安裝套件
  • 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. 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. More Perl…
  • 8.   CGI Perl is widely used for web programming
  • 9. 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.
  • 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. 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;
  • 12. Arrays
    • @animals = (“cat&quot;, “dog&quot;, “rabbit&quot;);
    • @nums = (11, 22, 33);
    • @mixed = (&quot;cat&quot;, 1.23);
  • 13. 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 = ();
  • 14. Complex data types
    • Reference: 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. Conditional constructs
  • 20. Looping constructs
    • while (cond ) { ... }
    • while (cond ) { ... } continue { ... }
    • Negated while statement
    • until (cond ) {…}
    • post-condition of while
    • {…} while (cond);
  • 21. Looping constructs
  • 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. Perl 6
    • http://www.perl6.org/

×