The Scripting Programming Language

Slides:



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

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.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
● Perl reference
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
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.
Programming and Perl for Bioinformatics Part III.
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.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 8: Perl Basics Fundamentals of Web Programming.
Basic Perl Programming
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue.
Print 'Hello world.'; Tren Griffith. Outline:  Perl introduction  Scalar Data  Variables  Operators  Control Structures  Input  Lists and Arrays.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
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)
Shell Script & Perl Programming Lesson 9. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any.
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.
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.
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.
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.
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Unix – CS 21
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CS 330 Class 9 Programming plan for today: More of how data gets into a script Via environment variables Via the url From a form By editing the url directly.
Perl Basics. sh-bang !!!! Every perl program starts with a sh-bang line #!/usr/bin/perl # hello.pl printf “Hello, world!\n”; printf STDOUT “Hello, world!\n”;
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
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.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
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.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Shell Script & Perl Programming
CSC 352– Unix Programming, Fall 2012
Perl Programming Language Design and Implementation (4th Edition)
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,
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
Presentation transcript:

The Scripting Programming Language PERL The Scripting Programming Language

Perl is a programming language. #!/usr/bin/perl 1 WHAT IS PERL ? Perl is a programming language. Unlike programming languages such as C and Fortran, Perl programs are scripts which direct the execution of the program “perl”. Perl execution is similar in form to shell script execution. Look at the introduction: http://perldoc.perl.org/perlintro.html

Perl is quick and easy to write. Perl is versatile: #!/usr/bin/perl 2 WHY USE PERL ? Perl is quick and easy to write. Perl is versatile: File/Sequence Management Command Line Execution Database GUI Webpage Easy manipulation of sequences in genetic sequence analysis

The script “Example.pl” is: #!/usr/bin/perl 3 BASIC SCRIPT The script “Example.pl” is: #!/usr/bin/perl $x = 1; $y = 1; $z = $x + $y; print “$x + $y = $z”; The execution of Example.pl is: > chmod +x Example.pl > ./Example.pl 1 + 1 = 2

WHAT ARE PERL VARIABLES ? #!/usr/bin/perl 4 WHAT ARE PERL VARIABLES ? Three main perl variables: Scalar : $name Array : @name Hash : %name

Scalar variables are common and versatile. #!/usr/bin/perl 5 PERL SCALAR Scalar variables are common and versatile. Scalar variables hold either numbers or strings $number = 12.4; $string = “Value is: “; print “$string$number\n”; $string = $string . “number 12”; print “$string\n”; Generates: Value is: 12.4 Value is: number 12

Array holds a list of values #!/usr/bin/perl 6 PERL ARRAY Array holds a list of values $value = “there”; @array = (“Hello”,$value,1); print “$arr[0] $arr[1] number $arr[2]\n”; $array[2] = 2; $array[3] = “again”; print “$arr[0] $arr[3] number “, $arr[2] – 1, “\n”; $size = @array; print “Array has size $size\n”; Generates: Hello there number 1 Hello again number 1 Array has size 4

Hash holds references to values #!/usr/bin/perl 7 PERL HASH Hash holds references to values %hash = ( “number” => 1, 34 => “Hello there” ); print $hash{34}, “ number “, $hash{“number”}, “\n”; @array = keys %hash; foreach $ref (@array){ print “Key: $ref = $hash{$ref}\n”; } Generates: Hello there number 1 Key number = 1 Key 34 = Hello there

Perl controls line execution with #!/usr/bin/perl 8 LINE CONTROL Perl controls line execution with if – elsif – else while for foreach With comparisons: For numbers: < (less than), > (greater than) <= (less than or equal), >= (greater than or equal) == (equal) For strings: eq (equal strings) ne (not equal strings)

IF – ELSIF - ELSE For example: #!/usr/bin/perl 9 if( $val < 2 ){ print “Value is less than 2\n”; } elsif( $val == 2 ){ print “Value is equal to 2\n”; else{ print “Value is greater than 2\n”; if( $stringA eq $stringB ){ print “Both strings are the same\n”; print “Both strings are different\n”;

WHILE, FOR LOOPS For example: #!/usr/bin/perl 10 # while( condition ) while( $i < 20 ){ print “The value $i is less than 20\n”; $i++; } # for( initial operation; condition; loop action ) for( $i = 0; $i < 20; $i++ ){ print “The value $i is greater or equal to 0\n”;

FOREACH LOOP For example: #!/usr/bin/perl 11 FOREACH LOOP For example: # foreach $value ( @array ) foreach $value ( @array ){ print “The value $value is in the array\n”; } foreach $value ( keys %hash ){ print “$value has value $hash{$value}\n”; Note that foreach loops traverse the array from low index to high index.

Array @ARGV holds command line arguments To open and read in a file: #!/usr/bin/perl 12 READING IN DATA Array @ARGV holds command line arguments To open and read in a file: open(IN,”$fileName”); $firstLine = <IN>; while(<IN>){ print $_; } close(IN); To write to a file: open(OUT,”> $fileName”); print OUT “Write this into $fileName\n”; close(OUT);

To read standard input: #!/usr/bin/perl 13 STANDARD STREAMS To read standard input: print “Write something: “; $value = <STDIN>; print “User wrote $value\n”; To write to standard output: print STDOUT “You see this on the command line\n”; To write to standard error: print STDERR “This is written as an error\n”;

The basic evaluation of regular expression are written as: #!/usr/bin/perl 14 REGULAR EXPRESIONS Regular expressions are a powerful tool for locating and modifying data The basic evaluation of regular expression are written as: $variable =~ /regular expression/ which returns true if the variable holds to the regular expression and false otherwise.

REG EXP EXAMPLES Example: #!/usr/bin/perl 15 if( $string =~ /Jonathan/ ){ print “The string contains Jonathan”; print “as a substring.\n”; } if( $string !~ /Myers/ ){ print “The string does not contain”; print “Myers as a substring”;

Basic elements in regular expressions: #!/usr/bin/perl 16 REG EXP Variables Basic elements in regular expressions: /./ = Any single character /\w/ = Any character a-z, A-Z, 0-9, and ‘_’ /\d/ = Any digit 0-9 /\s/ = space, tab, or enter /\./ = The character ‘.’ /\\/ = The character ‘\’ Definition of groups: /[xyz]/ = Occurrence of either x, y or z /[^xyz]/ = Occurrence of anything but x, y, or z Location of occurrence: /^x/ = Occurrence of x as the first character /x$/ = Occurrence of x as the last character

Number of occurrences: #!/usr/bin/perl 17 REG EXP Counts Number of occurrences: /x*/ = 0 or more copies of character ‘x’ in a row /x+/ = 1 or more copies of character ‘x’ in a row /x{3}/ = 3 copies of character ‘x’ in a row /(str)*/ = 0 or more copies of string ‘str’ in a row /$var*/ = 0 or more copies of the string $var in a row Example: $str = “a string with too much”; if( $str =~ /o{2}/ ){ print “True!\n”; } if( $str =~ /(crazy)*/ ){

#!/usr/bin/perl 18 Substitutions Substitutions are regular expressions that define replacements within strings. $str =~ s/(regular expression)/(replacement)/ Examples: $str = “aabbccddeeff”; $str =~ s/c/1/; print “$str\n”; $str =~ s/.$/2/; $str =~ s/^\w{3}/start /; Generates: aabb1cddeeff aabb1cddeef2 start b1cddeef2

Split function breaks a string into an array of strings #!/usr/bin/perl 19 SPLIT FUNCTION Split function breaks a string into an array of strings @array = split(/(regular expression)/,$string) Example: $str = “A set of strings”; @array = split(/\s+/,$str); for( $i = 0; $i < @array; $i++ ){ print “$array[$i]\n”; } Generates: A set of strings

REMEMBER PRACTICE!!!! We learn by doing !!!! Look at these sites!!! #!/usr/bin/perl 20 REMEMBER PRACTICE!!!! We learn by doing !!!! Look at these sites!!! http://perldoc.perl.org/perlintro.html http://www.perl.com/pub/a/2000/10/begperl1.html http://www.comp.leeds.ac.uk/Perl/start.html