Introduction to Perl Programming Morris Law December 8, 2012.

Slides:



Advertisements
Similar presentations
Perl Practical Extration and Reporting Language An Introduction by Shwen Ho.
Advertisements

A Guide to Unix Using Linux Fourth Edition
CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.
Bioinformatics is … - the use of computers and information technology to assist biological studies - a multi-dimensional and multi-lingual discipline Chapters.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
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.
Practical Extraction & Report Language Picture taken from
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/28.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Perl. Perl notes 2 Perl Perl - Practical extraction report language –for text files –system management –combines C, SED, AWK, SH –interpreted –dynamic.
Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Web Programming for DB Applications Yuen-Hsien Tseng 2006/04/18.
Introduction to Perl Giorgos Georgakilas Graduated from C.E.I.D.Graduated from C.E.I.D. M.Sc. degree in ITMBM.Sc. degree in ITMB Ph.D. student in DIANA-LabPh.D.
Introduction to Perl Yupu Liang cbio at MSKCC
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
An Overview of Perl A language for Systems and Network Administration and Management: An overview of the language.
Getting started in Perl: Intro to Perl for programmers Matthew Heusser – xndev.com - Presented to the West Michigan Perl User’s Group.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Scripting Languages Course 3 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
Scripting Languages Course 7 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
An Introduction to Perl – Part I
Perl Variables: Array Web Programming.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
The Selection Structure
INTRODUCTION to PERL PART 1.
Introduction to Python
Presentation transcript:

Introduction to Perl Programming Morris Law December 8, 2012

Content What is PERL? Installation and usage of PERL on Windows PERL number and strings Scalar, array and hash Operators Control Flow Regular Expression Subroutine File Use of Perl Package Manager

What is PERL? Practical Extraction and Report Language more than 1 way to do it easy for human reading highly portable talk text high level language free program code with.plx and.pl as extension on MS Windows and UNIX respectively

What is PERL? interpreter as well as compiler support unicode base 16 (hex) bundle with help:- –perldoc -f print (info) –perldoc -q reverse (faq) –perldoc Text::Wrap

PERL is free and portable Most UNIX/Linux distribution come with PERL installed Mac OS X already has PERL installed Windows does not come with PERL by default. Two choices –ActiveState PERL –Strawberry PERL Latest stable version is

Install and run ActiveState PERL Download from Run PERL Package Manager (PPM) to install and update standard and additional packages Start a command line interface ( cmd ) Prepare PERL source code (e.g. a.plx ) Run by > perl a.plx

Install and run DWIMPERL based on Strawberry PERL Download from Come with Padre, the Perl IDE

PERL numbers Number –255 (decimal) –0377 (octal) –0b (binary) –0xFF (hexidecimal) Example #!/usr/bin/perl #goodnums.plx use warnings; print 255, "\n"; print 0377, "\n"; print 0b , "\n"; print 0xFF, "\n"; goodnums.plx

PERL Strings single-quoted with no processing print '\t This is a $test \n'; double-quoted with interpolation $test = "mid term test"; print "\t This is a $test \n"; here-document print<<EOF; Dear Mary, I am demonstrating the usage of PERL EOF

PERL operators Arithmetics +,-,*,/,( ), **, % Bitwise &, |, ^, ~ Comparison ==, !=,, >=, <= gt, ge, eq, lt, le, cmp Boolean &&, ||, ! Repetition x3, x4

PERL operators (cont.) Ternary operator a ? b : c Range operator … Shift > regular expression =~, !~ a = b where = +,-,*,\,/

PERL scalar variables ($) scalar ($) –$user, $User, $b56, $_ my $name = "fred"; my $salutation = "Dear $name, "; print $salutation, "\n"; my $time = 8; print "This is my ${time}th time \n"; –Standard Input –Standard Output

PERL array variable = (12, 34, = qw/One Two Three = qw(Mon Tue Wed Thu Fri Sat Sun); ($mone, $mtwo) = (1, 3); ($mone, $mtwo) = ($mtwo, $mone); $a = (10, 20, 30)[0]; # 10 $a # Wed for $day { print $day, "\t"; } for (0 … $#array2) {print "Hello World\n"} for 2} print "After array2.plx

Use of pop and push in array pop : remove the top element of an array push : add elements to the top of = (1... = reverse (1... 6); push reverse1.plx

operators behaviour functions behave differently with operators take sort as an example # sort1.plx = (1, 2, 11, 24, 3, 36, 40, 4); = sort {$a cmp print "String \n"; = sort {$a print "Number \n";

PERL Hash variable (%) Hash : associative = ("Gary", "Dallas", "Lucy", "Exeter", "Ian", "Reading", "Samantha", "Oregon"); %where # # %where = (Gary => "Dallas", Lucy => "Exeter", Ian => "Reading", Samantha => "Oregon"); # %who = reverse (%where); # # %who = (Dallas => "Gary", Exeter => "Lucy", Reading => "Ian", Oregon => "Samantha" # print $where{Gary}, "\n"; print $who{Reading}, "\n"; delete $where{Lucy}; $where{Eva} = "Denver"; for (keys %where) { print "$_ lives in $where{$_}\n" } %who = reverse (%where); print $who{Denver}; print $where{Eva}; hash1.plx

PERL control flow if { … } elsif { … } else { … } #guess.plx $password = '1234'; my $guess = ; chomp $guess; if ($password ne $guess) { die "Go away, imposter!\n"; } else { die "Correct password!\n"; } unless ( ) { die “ message ” } # rate1.plx %currency = ( China => "RMB", Japan => "Yen", UK => "Pound", US => "Dollar" ); %rate = (RMB => 0.8, Yen => 10, Pound => 15, Dollar => 7.8); $to = ; chomp $to; unless (exists $rate{$to}) { die "I don't know anything about ${to} as a currency\n"; } # exists ($rate{to}) or die "I don't know anything about ${to} as a currency\n" print "The exchange rate of $to is $rate{$to}!"; guess.plx, rate1.plx

PERL control flow (cont) while ( ) { } do { } while ( ); for ( ) { } #testfor1.plx print "Enter number (1-3) : "; my $choice = ; chomp $choice; for ($choice) { $_ == 1 && print "You chose number ONE\n"; $_ == 2 && print "You chose number TWO\n"; $_ == 3 && print "You chose number THREE\n"; } testfor1.plx

PERL control flow (cont) foreach # foreach1.plx use strict; = qw/hat shoes shirts shorts/; foreach my $item { print "$item\n"; } # foreach2.plx use strict; my %table = qw/schmoe joe smith john simpson bart/; my($key, $value); # Declare two variables at once while ( ($key, $value) = each(%table) ) { print "$key and $value are friend\n"; }

Regular Expression RE use to describe patterns in text –a* represent zero or more ‘ a ’. the * character (repeat) –ba* will match ba, baab, baaac, bc the. character (any) –a.c will match a, a c, amc, abc, aac, apc, the | character (or) –abc|xyz will match abc or xyz group with () –xx(abc|xyz)*xx will match xxabcxx, xxxyzxx, xxabcxyzxx, etc.

RE – anchor and pattern matching ^a match the line preceding with a z$ match the line end with z What does ^aa*$ match? guess how ^a.*[0-9]$ match? test with the following use strict; while ( defined($currentLine = ) ) { if ($currentLine =~ /^aa*$/) { print $currentLine; }

PERL subroutine Usually PERL subroutine come in first capital Run with or without preceding &. use strict; sub HowdyEveryone { print "Hello guys.\nWhere do you want to go with Perl today?\n"; } &HowdyEveryone Run a subroutine with argument use strict; sub HowdyEveryone { my($name1, $name2) return "Hello $name1 and $name2.\n". "Where do you want to go with Perl today?\n"; } print &HowdyEveryone("bart", "lisa");

PERL subroutine subroutine accept as many arguments # greeting3.plx use strict; sub HowdyEveryone { my $returnString; foreach my $name { $returnString.= "$greeting, $name!\n"; } return $returnString. "Where do you want to go with Perl today?\n"; } print &HowdyEveryone("How is", "bart", "lisa", "homer", "marge", "maggie");

PERL file # numbering lines # nl.plx use warnings; use strict; open FILE, "example.txt" or die $!; my $lineno = 1; while ( ) { print $lineno++; print ": $_"; } nl.plx

Perl file A special notation <> stands for, argument input # Accept multiple files # nl2.plx use warnings; use strict; my $lineno = 1; while (<>) { print $lineno++; print ": $_"; } Run it with perl nl2.plx example.txt nl2.plx

Perl file # nl3.plx use warnings; use strict; my $lineno; my $current = ""; while (<>) { if ($current ne $ARGV) { $current = $ARGV; print "\n\t\tFile: $ARGV\n\n"; $lineno=1; } print $lineno++; print ": $_"; } nl3.plx

PERL input record separator $/ # fortune.plx use warnings; use strict; $/ = "\n%\n"; # input record separator open QUOTES, “bible.txt" or die $!; = ; my $random = my $fortune = $file[$random]; chomp $fortune; print $fortune, "\n"; fortune.plx

PERL file input & output # copy.plx use warnings; use strict; my $source = my $destination = open IN, $source or die "Can't read source file $source: $!\n"; open OUT, ">destination" or die "Can't write on file $destination: $!\n"; print "Copying $source to $destination\n"; while ( ) { print OUT $_; } copy.plx

More resources from CPAN using CPAN Explorer in DWIM

Thanks!Thanks! Any questions?