• Save
Introduction to Perl
Upcoming SlideShare
Loading in...5
×
 

Introduction to Perl

on

  • 1,283 views

 

Statistics

Views

Total Views
1,283
Views on SlideShare
1,283
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Introduction to Perl Introduction to Perl Presentation Transcript

    • 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/