Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.

Similar presentations


Presentation on theme: "Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes."— Presentation transcript:

1 Perl Language Yize Chen CS354

2 History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes a general purpose programming language -System administration -Web development -Network programming -GUI development, and more.

3 Features Perl takes many good features from other languages. The overall structure of Perl is from C. - Variables, expressions, assign statements, control structures and subroutines. Perl also adopts features from shell scripting languages. - Every variable has a leading sigils, - Lists, hashes and regular expressions

4 Features (cont.) Perl has many built-in functions that cover very large range of operations. Perl not only supports procedural programming and also supports object- oriented programming. Perl is widely used for web programming Perl works on multiple platforms

5 Perl Syntax Basic Script #!/usr/bin/perl $inputline = ; print($inputline); Data Types Perl has three main variable types: -scalars, arrays, and hashes -dynamically typed.

6 Scalars A scalar represents a single value: -string, integer or floating point numbers. -automatic conversion. $string = "hello"; $color = ‘green’; $num = 88; $float = 9.58;

7 Arrays @animals = (“cat", “dog", “rabbit"); @nums = (11, 22, 33); @mixed = ("cat", 1.23);

8 Hashes A hash is a set of key/value pairs: - %ages = ("li"=>18,“jin"=>21); - %same = ("li",18,“jin",21); - %mixed = (1=>“Mon", “flow"=>2.1) - %empty_hash = ();

9 Complex data types Reference: A scalar value $scalarref = \$num; $arrayref = \@mixed; $hashref = \%ages;

10 Complex data types - A reference can refer to any other Perl data type: $table = { “jin” => { age => 47; id => “teacher”, }, “mei” => { age => 21 id => “student”, }, “tom” => { age => 59 id=> “retired”, }, };

11 Operators Arithmetic Boolean logic + addition && and - subtraction || or * multiplication! not / division ** exponential

12 Comparison Numeric /String == /eq equal != /ne not equal < / lt less than > / gt greater than <= / le less than or equal >= / ge greater than or equal

13 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

14 Conditional constructs if (cond ) {... } if (cond ) {... } else {... } if (cond ) {... } elsif (cond ) {... } else {... } Negated if statement: unless cond {…} post-condition {…} if (cond)

15 Looping constructs while (cond ) {... } while (cond ) {... } continue {... } Negated while statement until (cond ) {…} post-condition of while {…} while (cond);

16 Looping constructs for ( init-expr ; cond-expr ; incr-expr ) {... } foreach var ( list ) {... } foreach var ( list ) {... } continue {... } {…} foreach var (list) a multi-way branch statement - given ( expr ) { when ( cond ) {... } default {... } }

17 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

18 Ruby vs. Perl Perl is good for simple procedural scripts Ruby is object-oriented programming. Every data type in Ruby is an object, which makes Ruby OOP syntax much simpler than Perl. Ruby is easier to maintain and read than Perl. Ruby is less documentation, less modules and fewer available libraries.

19 Evaluation (writability) Perl is easy to write, specially for little utilities - Very flexible syntax. -Provides many functionalities -“Perl makes easy things easy and hard things possible”

20 Evaluation (flexibility) Perl is very flexible. There is always more than one way to implement the code. while (<>) { next if /^#/; # skip the comment line. print; } print grep !/^#/, <>; print `grep -v '^#'`;

21 Evaluation (readability & maintenance) Perl is very difficult to read sometimes, specially to understand other one’s code. $/ = “ ”; While(<>) { while (m { \b (\w\S+) ( \s+ \1 ) + \b } xig ) { print “dup word ‘$1’ at paragraph $. \n”; } This readability problem makes it difficult to maintain other programmer’s code.

22 Evaluation (reliability) The scalar type has both strings and numbers. There is no way to check array indexing References to nonexistent elements return undef

23 Evaluation (cost) Development time is shorter and less cost. -Perl is a scripting language and has many flexible features. This makes it easier to learn and faster to write. The Perl is free software.

24 Reference http://en.wikipedia.org/wiki/Perl http://perldoc.perl.org/perlintro.html Programming Perl, O'Reilly Media,Programming PerlO'Reilly Media


Download ppt "Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes."

Similar presentations


Ads by Google