19-Jun-15 Perl Major parts of this lecture adapted from

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

A Guide to Unix Using Linux Fourth Edition
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
Introduction to C Programming
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
1 Perl Tutorial Adapted from slides found at: original author is not indicated Bioinformatics Orientation 2008 Eric Bishop.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
1 Perl Tutorial Practical extraction and report language
17-Jun-15 Perl Major parts of this lecture adapted from
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
Practical Extraction & Report Language Picture taken from
23-Jun-15 Perl 4 and 5 Major parts of this lecture adapted from
Guide To UNIX Using Linux Third Edition
Scripting Languages Chapter 8 More About Regular Expressions.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Basic Elements of C++ Chapter 2.
COMP Week 7 Dr. Chunbo Chu. Introduction to Perl Practical Extraction and Report Language Created by Larry Wall in the mid-1980s Uses Administration.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
An Introduction to Unix Shell Scripting
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
2440: 211 Interactive Web Programming Expressions & Operators.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Sed, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
Built-in Data Structures in Python An Introduction.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
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.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
COMP519: Web Programming Autumn 2010 Perl Tutorial: Beyond the Basics  Keyboard Input and file handling  Control structures  Conditionals  String Matching.
Python 1 SIGCS 1 Intro to Python March 7, 2012 Presented by Pamela A Moore & Zenia C Bahorski 1.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
The Scripting Programming Language
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Review of Basic Perl and Perl Regular Expressions Alexander Fraser & Liane Guillou CIS, Ludwig-Maximilians-Universität.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
String and Lists Dr. José M. Reyes Álamo.
Introduction to perl programming: the minimum to know for practice!
Pamela Moore & Zenia Bahorski
Lecture 9 Shell Programming – Command substitution
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Introduction to Python
Introduction to C++ Programming
PHP.
Presentation transcript:

19-Jun-15 Perl Major parts of this lecture adapted from

2 Why Perl? Perl is built around regular expressions REs are good for string processing Therefore Perl is a good scripting language Perl is especially popular for CGI scripts Perl makes full use of the power of UNIX Short Perl programs can be very short “Perl is designed to make the easy jobs easy, without making the difficult jobs impossible.” -- Larry Wall, Programming Perl

3 Why not Perl? Perl is very UNIX-oriented Perl is available on other platforms......but isn’t always fully implemented there However, Perl is often the best way to get some UNIX capabilities on less capable platforms Perl does not scale well to large programs Weak subroutines, heavy use of global variables Perl’s syntax is not particularly appealing

4 What is a scripting language? Operating systems can do many things copy, move, create, delete, compare files execute programs, including compilers schedule activities, monitor processes, etc. A command-line interface gives you access to these functions, but only one at a time A scripting language is a “wrapper” language that integrates OS functions

5 Major scripting languages UNIX has sh, Perl Macintosh has AppleScript, Frontier Windows has no major scripting languages probably due to the weaknesses of DOS Generic scripting languages include: Perl (most popular) Tcl (easiest for beginners) Python (new, Java-like, best for large programs)

6 Perl versions To find out which version of Perl you are using, type perl -v at the command line Alternatively, put the following in a file version.pl #!/usr/bin/perl print "\n\nHello from Perl $] !\n\n"; Run this program with perl version.pl This may or may not work, depending on where and how Perl is located on your system Please use Perl 5 in your assignments I’m using Perl 5.8.8

7 Perl Example 1 #!/usr/local/bin/perl # # Program to do the obvious # print 'Hello world.'; # Print a message Save this on a file named hello.pl

8 Comments on “Hello, World” Perl statements end with semicolons Perl is case-sensitive Perl is compiled and run in a single operation Comments are # to end of line But the first line, #!/usr/local/bin/perl, tells where to find the Perl compiler on your system It’s usually here or at /usr/bin/perl Perl files should have the.pl extension How to run the hello.pl Perl program: perl hello.pl may work, and may or may not require the first #! line./hello.pl (or.\hello.pl ) may work, and requires the first #! line

9 Perl in EasyEclipse Open the Perl perspective Window -> Open Perspective -> Other... -> Perl Create a Perl Project File -> New -> Project... -> Perl -> Perl Project File -> New -> Perl Project (if in the Perl perspective) Create a file, and give it the.pl extension File -> New -> Perl File Write your Perl code Create a run configuration Run -> Run... -> Perl Local -> New_configuration In the Main tab: Name : enter a meaningful name (in place of “new_configuration” Project :Browse, choose project File to execute : choose file Apply and Run

10 Perl Example 2 #!/ex2/usr/bin/perl # Remove blank lines from a file # Usage: singlespace newfile while ($line = ) { if ($line eq "\n") { next; } print "$line"; }

11 More Perl notes On the UNIX command line; < filename means to get input from this file > filename means to send output to this file In Perl, is the input file, is the output file Scalar variables start with $ Scalar variables hold strings or numbers, and they are interchangeable Examples: $priority = 9; $priority = '9'; Array variables start

12 Perl Example 3 #!/usr/local/bin/perl # Usage: fixm # Replace \r with \n -- replaces input files foreach $file { print "Processing $file\n"; if (-e "fixm_temp") { die "*** File fixm_temp already exists!\n"; } if (! -e $file) { die "*** No such file: $file!\n"; } open DOIT, "| tr \'\\015' \'\\012' fixm_temp" or die "*** Can't: tr '\015' '\012' $outfile\n"; close DOIT; open DOIT, "| mv -f fixm_temp $file" or die "*** Can't: mv -f fixm_temp $file\n"; close DOIT; }

13 Comments on example 3 In # Usage: fixm, the angle brackets just mean to supply a list of file names here In UNIX text editors, the \r (carriage return) character usually shows up as ^M (hence the name fixm_temp ) The UNIX command tr '\015' '\012' replaces all \015 characters ( \r ) with \012 ( \n ) characters The format of the open and close commands is: open fileHandle, fileName close fileHandle, fileName "| tr \'\\015' \'\\012' fixm_temp" says: Take input from $file, pipe it to the tr command, put the output on fixm_temp

14 Arithmetic in Perl $a = 1 + 2; # Add 1 and 2 and store in $a $a = 3 - 4; # Subtract 4 from 3 and store in $a $a = 5 * 6; # Multiply 5 and 6 $a = 7 / 8; # Divide 7 by 8 to give $a = 9 ** 10; # Nine to the power of 10, that is, 9 10 $a = 5 % 2; # Remainder of 5 divided by 2 ++$a; # Increment $a and then return it $a++; # Return $a and then increment it --$a; # Decrement $a and then return it $a--; # Return $a and then decrement it

15 String and assignment operators $a = $b. $c; # Concatenate $b and $c $a = $b x $c; # $b repeated $c times $a = $b; # Assign $b to $a $a += $b; # Add $b to $a $a -= $b; # Subtract $b from $a $a.= $b; # Append $b onto $a

16 Single and double quotes $a = 'apples'; $b = 'bananas'; print $a. ' and '. $b; prints: apples and bananas print '$a and $b'; prints: $a and $b print "$a and $b"; prints: apples and bananas

17 = ("apples", "bananas", "cherries"); But… print $food[1]; prints == ("meat", "apples", "bananas", "cherries"); ($a, $b, $c) = (5, 10, 20);

18 push and pop push adds one or more things to the end of a list push "eggs", "bread"); push returns the new length of the list pop removes and returns the last element $sandwich = $len # $len gets length $#food # returns index of last element

19 foreach # Visit each item in turn and call it $morsel foreach $morsel { print "$morsel\n"; print "Yum yum\n"; }

20 Tests “Zero” is false. This includes: 0, '0', "0", '', "" Anything not false is true Use == and != for numbers, eq and ne for strings &&, ||, and ! are and, or, and not, respectively.

21 for loops for loops are just as in C or Java for ($i = 0; $i < 10; ++$i) { print "$i\n"; }

22 while loops #!/usr/local/bin/perl print "Password? "; $a = ; chop $a; # Remove the last character (\n) while ($a ne "fred") { print "sorry. Again? "; $a = ; chop $a; }

23 do..while and do..until loops #!/usr/local/bin/perl do { print "Password? "; $a = ; chop $a; } while ($a ne "fred");

24 if statements if ($a) { print "The string is not empty\n"; } else { print "The string is empty\n";

25 if - elsif statements if (!$a) { print "The string is empty\n"; } elsif (length($a) == 1) { print "The string has one character\n"; } elsif (length($a) == 2) { print "The string has two characters\n"; } else { print "The string has many characters\n"; }

26 Why Perl? Two factors make Perl important: Pattern matching/string manipulation Based on regular expressions (REs) REs are similar in power to those in Formal Languages… …but have many convenience features Ability to execute UNIX commands The Perl interpreter emulates these commands on non- UNIX platforms Often Perl is used simply for its UNIX emulation

27 Basic pattern matching $sentence =~ /the/ True if $sentence contains "the" $sentence = "The dog bites."; if ($sentence =~ /the/) # is false …because Perl is case-sensitive !~ is "does not contain"

28 RE special characters. # Any single character except a newline ^ # The beginning of the line or string $ # The end of the line or string * # Zero or more of the last character + # One or more of the last character ? # Zero or one of the last character

29 RE examples ^.*$ # matches the entire string hi.*bye # matches from "hi" to "bye" inclusive x +y # matches x, one or more blanks, and y ^Dear # matches "Dear" only at beginning bags? # matches "bag" or "bags" hiss+ # matches "hiss", "hisss", "hissss", etc.

30 Square brackets [qjk] # Either q or j or k [^qjk] # Neither q nor j nor k [a-z] # Anything from a to z inclusive [^a-z] # No lower case letters [a-zA-Z] # Any letter [a-z]+ # Any non-zero sequence of # lower case letters

31 More examples [aeiou]+ # matches one or more vowels [^aeiou]+ # matches one or more nonvowels [0-9]+ # matches an unsigned integer [0-9A-F] # matches a single hex digit [a-zA-Z] # matches any letter [a-zA-Z0-9_]+ # matches identifiers

32 More special characters \n # A newline \t # A tab \w # Any alphanumeric; same as [a-zA-Z0-9_] \W # Any non-word char; same as [^a-zA-Z0-9_] \d # Any digit. The same as [0-9] \D # Any non-digit. The same as [^0-9] \s # Any whitespace character \S # Any non-whitespace character \b # A word boundary, outside [] only \B # No word boundary

33 Quoting special characters \| # Vertical bar \[ # An open square bracket \) # A closing parenthesis \* # An asterisk \^ # A carat symbol \/ # A slash \\ # A backslash

34 Alternatives and parentheses jelly|cream # Either jelly or cream (eg|le)gs # Either eggs or legs (da)+ # Either da or dada or # dadada or...

35 Substitution =~ is a test, as in: $sentence =~ /the/ !~ is the negated test, as in: $sentence !~ /the/ =~ is also used for replacement, as in: $sentence =~ /london/London/ This is an expression, whose value is the number of substitutions made (0 or 1)

36 The $_ variable Often we want to process one string repeatedly The $_ variable holds the current string If a subject is omitted, $_ is assumed Hence, the following are equivalent: if ($sentence =~ /under/) … $_ = $sentence; if (/under/)...

37 Global substitutions s/london/London/ substitutes London for the first occurrence of london in $_ s/london/London/g substitutes London for each occurrence of london in $_ The value of a substitution expression is the number of substitutions actually made

38 Case-insensitive substitutions s/london/London/i case-insensitive substitution; will replace london, LONDON, London, LoNDoN, etc. You can combine global substitution with case- insensitive substitution s/london/London/gi

39 Remembering patterns Any part of the pattern enclosed in parentheses is assigned to the special variables $1, $2, $3, …, $9 Numbers are assigned according to the left (opening) parentheses "The moon is high" =~ /The (.*) is (.*)/ Afterwards, $1 = "moon" and $2 = "high"

40 Dynamic matching During the match, an early part of the match that is tentatively assigned to $1, $2, etc. can be referred to by \1, \2, etc. Example: \b.+\b matches a single word /(\b.+\b) \1/ matches repeated words "Now is the the time" =~ /(\b.+\b) \1/ Afterwards, $1 = "the"

41 tr tr does character-by-character translation tr returns the number of substitutions made $sentence =~ tr/abc/edf/; replaces a with e, b with d, c with f $count = ($sentence =~ tr/*/*/); counts asterisks tr/a-z/A-Z/; converts to all uppercase

42 split split breaks a string into parts $info = "Caine:Michael:Actor:14, Leafy = split(/:/, = ("Caine", "Michael", "Actor", "14, Leafy Drive");

43 Associative arrays Associative arrays allow lookup by name rather than by index Associative array names begin with % Example: %fruit = ("apples", "red", "bananas", "yellow", "cherries", "red"); Now, $fruit{"bananas"} returns "yellow" Note: braces, not parentheses

44 Associative Arrays II Can be converted to normal = %fruit; You cannot index an associative array, but you can use the keys and values functions: foreach $f (keys %fruit) { print ("The color of $f is ". $fruit{$f}. "\n"); }

45 Associative Arrays III The function each gets key-value pairs while (($f, $c) = each(%fruit)) { print "$f is $c\n"; }

46 Calling subroutines Assume you have a subroutine printargs that just prints out its arguments Subroutine calls: &printargs("perly", "king"); Prints: "perly king" &printargs("frog", "and", "toad"); Prints: "frog and toad"

47 Defining subroutines Here's the definition of printargs : sub printargs { print } Where are the parameters? Parameters are put in has nothing to do with $_ ; they are unrelated

48 Returning a result The value of a subroutine is the value of the last expression that was evaluated sub maximum { if ($_[0] > $_[1]) { $_[0]; } else { $_[1]; } } $biggest = &maximum(37, 24);

49 Local is local to the subroutine, and… …so are $_[0], $_[1], $_[2], … local creates local variables

50 Example subroutine sub inside { local($a, $b); # Make local variables ($a, $b) = ($_[0], $_[1]); # Assign values $a =~ s/ //g; # Strip spaces from $b =~ s/ //g; # local variables ($a =~ /$b/ || $b =~ /$a/); # Is $b inside $a # or $a inside $b? } &inside("lemon", "dole money"); # true

51 Perl 5 Perl 5 is usually described as “a whole new language” However, Perl 5 is mostly backward compatible, and there are only a few apparent differences The most significant difference is that Perl can now be written in a more object-oriented fashion (like C++ compares to C) Perl 4 had three types of data: and %hash Perl 5 adds another item: the reference References are indicated by \ Perl 5 interpolates arrays into double-quoted strings Perl 5 provides reluctant quantifiers (with ? ) Perl 5 has modules, which are similar to classes Perl now provides a handful of modules (just as Java has always provided prewritten classes) Perl 5 has “auto” variables Variables may now be declared within a lexical scope

52 Perl 6 Whereas Perl 5 is often described as “a whole new language,” Perl 6 is a whole new language The best summary of the differences that I’ve found is Perl 6 status: Vaporware? Under development since 1999 or 2000 Larry Wall: “We're working on it, slowly but surely...or not- so-surely in the spots we're not so sure...” Just interesting reading: An interview with Larry Wall:

53 The End