Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.

Slides:



Advertisements
Similar presentations
1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
A Guide to Unix Using Linux Fourth Edition
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
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.
Programming and Perl for Bioinformatics Part I. A Taste of Perl: print a message perltaste.pl: Greet the entire world. #!/usr/bin/perl #greet the entire.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
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.
Practical Extraction & Report Language Picture taken from
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Guide To UNIX Using Linux Third Edition
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Practical Extraction & Report Language PERL Joseph Beltran.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
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 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
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
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.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Perl Practical(?)‏ Extraction and Report Language.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
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 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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
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
5 1 Data Files CGI/Perl Programming By Diane Zak.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
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.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Multimedia- and Web-based Information Systems Lecture 6.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
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.
Scripting Languages ● Perl, Python, Unix Shell, VB, JavaScript, etc. ● Glue Languages. ● Typeless. ● Interchangeable Code and Data. ● Interpreted vs. Compiled.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
CSC 352– Unix Programming, Fall 2012
Perl Variables: Array Web Programming.
Control Structures: for & while Loops
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
CSC 352– Unix Programming, Fall, 2011
Introduction to Bash Programming, part 3
Presentation transcript:

Perl: Lecture 1 The language

What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting language – Ability to change everything during runtime – Fast to employ, one-liners possible – Slower than C

What Perl is Easy to learn – Learning curve similar to human language – More difficult things possible Tell it to be more strict Object orientation Esp. suited for the web & text processing – Regular expressions

How to get & use it – ActiveState makes port for Microsoft Windows Current Version is Comprehensive documentation included – C:\Perl\html\index.html – Perldoc Running perl scripts – perl –w script.pl – #!/usr/bin/perl + chmod (Unix)

Variables Scalars$ Hash% No need to declare variables Namespace for each variable type Case sensitive

Scalars Number String Reference Automatic conversion of numbers and strings $i = 1; $j = "2"; print "$i\n"; print "$j\n"; $k = $i + $j; print "$k\n"; print $i. $j. "\n";

Scalar Comparison Operators Number String ==eg <>ne <lt >gt <=le =>ge

Truth Any String is true except for „“ and „0“ Any number is true except for 0 Any reference is true Any undefined variable is false

The Perl if statement if ($var==1) { commands1; } elsif ($var==2){ commands2; } else{ commands3; } unless ($var==3) {commands4; }

Arrays Multivalued Variable Lookup by number List Assignments = ("couch", "chair", "table", "stove") ($one, $two, $three, $four) ($one, $two) = ($two, $one) $home[0] - $home[3]

Hashes Multivalued Variable Lookup by name List Assignments Accessing %longday = ("Sun" => "Sunday", "Mon" => "Monday", "Tue" => "Tuesday" = %longday $longday{"Sun"}, $longday{"Mon"}

Quoting in Perl Double Quotes ““ interprete variables and backslashes Single Quotes ‘‘ don‘t Own Quoting characters q//, qq// $one=“two”; $two=“four”; print ‘$one + $one is $two \n’; print “$one + $one is $two \n”;

Operators (1) Arithmetic String Logical %Modulus **Exponentiation.String concatenation xRepeat operator &&, andAND ||, orOR !, notNOT

Operators (2) File test operators -eExists -rReadable -wWritable -dIs a directory -fIs a regular file -TIs a Text file

Iterative Structures For While Foreach while ($x<0.5) { $x = rand; } for ($x=0; $x<10; $x++) { print “$x\n“; } foreach $line { if ($line eq ““) last; print reverse $line; }

Next and Last next ( continue in C) : start the next iteration of the loop. last ( break in C) : exit the loop.

Working with Files open FILEHANDLE, EXPRESSION – Filehandle : means to access opened file – Expression : path of file to open, file mode „<“read (default) „>“ truncate and write „>>“append close FILEHANDLE open LOG, ‘>>/var/log/mylog‘; close LOG;

Input and Ouput Print FILEHANDLE OUTPUT „ “ line reading operator print LOG “Debug Message“; $line = = ; while ($line = ) { print $line; }

Default Variable „$_“ default variable for functions $_ = „222“; print log; print while(<>);

Subroutines Equivalent of C functions Argument list, result list, no need to specify length sub NAME {}# declaration &NAME();# invocation sub arguments { =

Subroutines 2 „Named Parameters“ sub printtimes { my $string = shift; my $times = shift; while ($times-->0) { print $string; } $printtimes(„BA Stuggi\n“, 10);

Subroutines bonus for the main program : if (defined $ARGV[0]) { $loga = log $ARGV[0]; print $loga; } print log (shift or exit);

Perl Regular Expressions (1) (perlrequick)

Simple Pattern Matching =~ Operator : boolean – TRUE for a match – FALSE for no match "Hello World" =~ /World/; # matches regex delimiters expression to match

Simple Pattern Matching Usage 1.if ("Hello World" =~ /World/) print "It matches\n " ; 2.if ("Hello World" !~ /World/) print " No match\n " ; 3.$teststring= " Hello World "; $greeting = "World"; if ($teststring =~ /$greeting/) print "It matches\n " ; 4.$_ = "Hello World"; if (/World/) print "It matches\n";

Simple Pattern Matching Bonus Delimiters changable if „m“ is introduced Match always at the earliest point possible Special characters („metacharacters“) require masking "Hello World" =~ m!World!; # matches {}[]()^$.|*+?\ "2+2=4" =~ /2\+2/; # matches, 'C:\WIN32' =~ /C:\\WIN/; # matches

Scalar Manipulation (1) chop VAR / chomp VAR – Remove last character / Only if newline and return it lc EXPR – Returns a lowercased EXPR length EXPR – Returns number of characters in EXPR index STR, SUBSTR [, POS] – Returns first position of SUBSTR in STR [after POS] rindex STR, SUBSTR [,POS] – See above, uses last occurence

Scalar Manipulation (2) sprintf FORMAT, LIST %c a character with the given number %sa string %d a signed integer, in decimal %u an unsigned integer, in decimal %x an unsigned integer, in hexadecimal %f a floating-point number, in fixed decimal notation substr EXPR,OFFSET [,LENGTH] – return the substring of EXPR starting from OFFSET uc EXPR – Returns an uppercased EXPR