Introduction to perl Research Computing/Larry Mason February 17, 2009.

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.
A Guide to Unix Using Linux Fourth Edition
● Perl reference
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
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.
CSC3530 Software Technology Tutorial Two PERL Basics.
CS 898N – Advanced World Wide Web Technologies Lecture 10: Examples of PERL and CGI Chin-Chih Chang
Perl Programming WeeSan Lee
 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
Sort the Elements of an Array Using the ‘sort’ keyword, by default we can sort the elements of an array lexicographically. Elements considered as strings.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
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
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
13 More Advanced Awk Mauro Jaskelioff (originally by Gail Hopkins)
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl II Part III: Motifs and Loops. Objectives Search for motifs in DNA or Proteins Interact with users at the keyboard Write data to files Use loops.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Computer Programming for Biologists Class 6 Nov 21 th, 2014 Karsten Hokamp
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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 12/13/ Introduction to Perl - Strings, Truth and Regex Introduction to Perl Session 2 · manipulating.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
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 Five Agenda Link of the week Review week four 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)
Sed. Class Issues vSphere Issues – root only until lab 3.
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.
Part 4 Arrays: Stacks foreach command Regular expressions: String structure analysis and substrings extractions and substitutions Command line arguments:
The Scripting Programming Language
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.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Introduction to Scripting Workshop February 23, 2016.
Perl Variables: Array Web Programming.
Lesson 2. Control structures File IO - reading and writing Subroutines
Linux Shell Script Programming
Chapter 5 Bourne Shells Scripts
Presentation transcript:

Introduction to perl Research Computing/Larry Mason February 17, 2009

its.unc.edu 2 Class Material Point web browser to: Click on “Training” (in left column) Click on “ITS Research Computing Training Presentations” (Bottom paragraph) Click on “Getting Started on Perl” (under General Computing)

its.unc.edu 3 ssh to linux host To start ssh using SecureCRT in Windows: Start  Programs  [Remote Services]  SecureCRT Click the Quick Connect icon at the top. (Second icon from left?) Hostname: emerald.isis.unc.edu Login with your ONYEN and password Create a directory for this course mkdir perl_intro [or other name of your choice] cd perl_intro

its.unc.edu 4 Bring in class files On the linux host (emerald?) in perl_intro: cp /afs/isis/depts/its/public_html/divisions /rc/training/scientific/perl_Intro/exampl es/*. ls –l

its.unc.edu 5 Running perl scripts ls –l 001hello.pl Shows permissions in left column -rwxr-xr-x means the script can be run by everybody (the “x” means “executable”) chmod 700 *.pl To make all perl scripts runable / executable by you, the owner

its.unc.edu 6 #!/afs/isis/pkg/isis/bin/perl –w print STDOUT “Hello, world.”; print STDOUT “Hello, world.\n”; print STDOUT ‘Hello, world.\n’; print STDOUT “\n\n”; 1) Hello, World!

its.unc.edu 7 2) Various Variables #!/afs/isis/pkg/isis/bin/perl –w $string = “Hello,”. “ world.\n”; # concatenation print STDOUT “$string”; $number = 2009; print STDOUT “the number is $number\n”; $number[0] = “one”; $number[1] = “2”; $number[2] = “three”; print STDOUT

its.unc.edu 8 3) pragmas #!/afs/isis/pkg/isis/bin/perl –w use strict; my $string = “/tmp/fileout.$$”; … # Note the error messages.

its.unc.edu 9 4) Variable flexibility #!/afs/isis/pkg/isis/bin/perl –w use strict; my $file = “/tmp/fileout.$$”; my $string = “Hello, world”; … $number[0] = 14; print STDOUT “$number[0]\n”; $number[0] = substr($string,0,5); print STDOUT “$number[0]\n”; exit; …

its.unc.edu 10 5) String or Number #!/afs/isis/pkg/isis/bin/perl –w … my $string = “ ”; … $number[0] = substr($string,-5,3); print STDOUT “$number[0]\n”; print STDOUT “$string\n”; $number[0] = $string * 3; print STDOUT “$number[0]\n”; $string = “abc”; $number[0] = $string * 3; print STDOUT “$number[0]\n”;

its.unc.edu 11 6) Files: in and out #!/afs/isis/pkg/isis/bin/perl –w my $string = “/tmp/fileout.$$”; my $number = 2009; my $record = “”; = (“one”, “2”, “three”); open OUTFILE, “>$string”; print OUTFILE “$number\nnumber[1] is $number[1]\nnumber[2] is $number[2]\n\nOUTFILE is $string”; close OUTFILE; open SAM, “<$string”; $record = ; print STDOUT “$record”;

its.unc.edu 12 7) Loops: while #!/afs/isis/pkg/isis/bin/perl –w... open SAM, “<$string”; while ( ) { print STDOUT “$_”; } close SAM;

its.unc.edu 13 8) chop and chomp #!/afs/isis/pkg/isis/bin/perl –w... open SAM, “<$string”; while ( ) { print STDOUT “$_”; chomp $_; print STDOUT “$_”; chop $_; print STDOUT “$_”; } # close of SAM unnecessary here because program ends here

its.unc.edu 14 9)Loops: for #!/afs/isis/pkg/isis/bin/perl –w... my $last = $#number; for ($i = 0; $i <= $last; $i++) { print OUTFILE “$number[$i]\n”; } # end for each element of array close OUTFILE; open SAM,”<$string”; while ( ) { print STDOUT “$_”; } # end while records from file SAM

its.unc.edu 15 10) Next #!/afs/isis/pkg/isis/bin/perl –w my $count = 0; … close ANYFILE; open ANYFILE, “<$file”; while ( ) { chomp $_; $count++; if ( $_ gt “one” ) { next; } print STDOUT “now count is $count\n”; } # end while ANYFILE records print STDOUT “Final count is $count\n”;

its.unc.edu 16 11) Last #!/afs/isis/pkg/isis/bin/perl –w … CASE: while ( ) { chomp $_; $count++ if ( $_ eq “two” ) { next CASE;} elsif ( $_ ne “one” ) { print STDOUT “What\’s $_ doing here?\n”; last; } else { print STDOUT “It\’s only $_\n”; } # end else found a one print STDOUT “Record number $count is $_\n”; } # end while ANYFILE records print STDOUT “Final count is $count\n”;

its.unc.edu 17 12) hashes #!/afs/isis/pkg/isis/bin/perl –w my $var = “rosebud”; my %number; $number{one} = “one”; $number{‘two’} = “something”; $number{“molly”} = 3; $number{$var} = “4”; print STDOUT “%number\n”; print STDOUT “$number{molly} and $number{rosebud}\n”;

its.unc.edu 18 13)loops: foreach #!/afs/isis/pkg/isis/bin/perl –w … my $var = “”; my %number = (‘one’,1,’two’,2,’molly’,3,’rosebud’,4); open ANYFILE, “>$string”; foreach $var ( keys %number ) { print ANYFILE “$number{$var}\n”; } # end foreach index key of %number print STDOUT “$string\n”; # check the contents of the output file $string

its.unc.edu 19 Functions: sub $result = &mysub ($param); sub mysub { my $something = $_[0]; return; } Could use “return $something;” with the same result. Value of last expression evaluated is returned by default. Parameters are passed to the subroutine in the Changing $_[0] would change the value of $param. Subroutine has access to global variables.

its.unc.edu 20 14) sorting #!/afs/isis/pkg/isis/bin/perl –w … open ANYFILE, “>$string”; foreach $var ( sort keys %number ) { print ANYFILE “$number{$var}\n”; } # end foreach sorted index key of %number print STDOUT “$string\n”; # what did it sort by?

its.unc.edu 21 15) sorting by Value #!/afs/isis/pkg/isis/bin/perl –w … sub byvalue { $number{$a} $number{$b}; } open ANYFILE, “$string”; foreach $var ( sort byvalue keys %number ) { print ANYFILE “$number{$var}\n”; } # end foreach index, sorted by value, keys of %number print STDOUT “$string\n”; # Now check the sequence of records in $string # sub byvalue { $number{$b} $number{$a}; } # sub byvalue { $number{$a} cmp $number{$b}; }

its.unc.edu 22 16) Command Input #!/afs/isis/pkg/isis/bin/perl –w … $file = “/tmp/fileout\*”; $string = `/usr/bin/wc $file`; # all in one = `/usr/bin/wc $file`; # each record one element print STDOUT “STRING is $string\n”; print STDOUT “NUMBER exit; …

its.unc.edu 23 17) More Command Input #!/afs/isis/pkg/isis/bin/perl –w... my $var = “”; my $file = “/tmp/fileout\*”; $var = “/bin/ls $file”; open LS, “$var |”; while ( ) { print STDOUT “$_”; } # end while ls command output

its.unc.edu 24 18) Parsing Input #!/afs/isis/pkg/isis/bin/perl –w = `/usr/bin/wc $file`; print STDOUT “INPUT = split(‘ ‘,$number[$count]); print STDOUT “ALL F[0] $F[0] F[2] $F[2] F[4] = split(‘ ‘,$number[$count],3); print STDOUT “THREE F[0] $F[0] F[2] $F[2]\n”; $#F = 1; print STDOUT “Array of two F[0] $F[0] F[2] $F[2]\n”; exit;

its.unc.edu 25 19) More Parsing #!/afs/isis/pkg/isis/bin/perl –w … my $this = my $that = my $trash =0; print STDOUT “INPUT $number[0]\n”; $#F = -1; print STDOUT “F[0] = split(/\//,$number[0]); print STDOUT “F[0] $F[0] F[1] $F[1] F[2] $F[2]\n”; ($this, $that, $trash) = split(/tmp/,$number[0],3); print STDOUT “this $this that $that trash $trash\n”; exit;

its.unc.edu 26 Functions:Pattern Matching m/PATTERN/gimosx m#PATTERN#gimosx metacharacters \ | ( ) [ { ^ $ * + ?. “^” matches beginning of string “$” matches end of string “\n” newline character “\t” tab character “\d” digit (0-9) “\D” non-digit “\s” whitespace character “\S” non-whitespace chatacter

its.unc.edu 27 20) Regular Expressions II #!/afs/isis/pkg/isis/bin/perl –w my $one = my $five = my $fifteen = 0; ($one, $five, $fifteen) = (`/usr/bin/uptime` =~ /(\d+\.\d+)/g); print STDOUT “one is $one, five is $five, fifteen is $fifteen\n”; my $paragraph = “”; my $sentences = 0; $/ = “”; while ($paragraph = <>) { while ($paragraph =~ /[a-z][‘”)]*[.!?]+[‘”)]*\s/g) { $sentences++; } } print STDOUT “$sentences\n”;

its.unc.edu 28 Functions: die open OUTFILE, “>$file” or die; open OUTFILE, “>$file” or die “Could not open $file, stopped “; Perl will add “at [pgmname] line [N].” to the die output.

its.unc.edu 29 21) Functions: getpwent #!/afs/isis/pkg/isis/bin/perl –w my $name = my $passwd = my $uid = $gid = “”; my $quota = my $comment = my $gcos = “”; my $dir = my $shell = “”; my %uid; ($name, $passwd, $unid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwent; print STDOUT “name = $name and shell = $shell\n”; while (($name, $passwd, $uid) = getpwent) { $uid{$name} = $uid; # add to hash } # end while records in /etc/passwd

its.unc.edu 30 22) Functions: index, length #!/afs/isis/pkg/isis/bin/perl –w my $here = my $position = 0; my $string = “abcdefghi”; my $substr = “def”; $here = index ($string, $substr, $position); print STDOUT “$substr is at position $here in $string\n”; $here = length ($string); print STDOUT “The string is $here in length.\n”;

its.unc.edu 31 23) Functions: time my $second = my $min = my $hour = my $mday = my $mon = 0; my $year = my $wday = my $yday = my $isdst = 0; ($second, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime (time); January is month “0”. The year needs 1900 added to it. Sunday is wday “0”. Is Daylight Savings Time is zero for EST. “time” returns the seconds since the Epoch.

its.unc.edu 32 Various functions sleep 60; # wait for 60 seconds before going on. system “$command”; # executes the string in $command # in a forked bourne shell and waits for it to finish. tr/[searchlist/replacementlist/cds; $result =~ tr/A-Z/a-z/; # Translates the string in $result # uppercase letters changed to lower case. # output stored in $result. # “c” after “///” means to compliment the searchlist # “d” means delete characters not replaced # “s” means print only one of a sequence all the same undef %H; undef $string; # undefines things.

its.unc.edu 33 Modules: example use strict; use Getopt::Std; use vars qw($opt_u, $opt_r, $opt_s, $opt_g); getopts(‘u:rsg:’); # colon means parameters “u” and “g” have arguments. # each causes a variable with name “$opt_” prefix. # If r is used the $opt_r variable will be “1”, else “0”. # The “use vars” is a “pragma” that allows this to work with # use strict. # the “qw” is “quote words” and is a way to present a list.

its.unc.edu The End