introduction to perl
DESCRIPTION
Introduction to Perl. Thaddeus Aid IT Learning Programme University of Oxford 15/04/2014. About the course. In these three sessions we will cover: Basic Programming Concepts Perl Syntax Data input, manipulation, and output - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/1.jpg)
Introduction to PerlThaddeus Aid
IT Learning ProgrammeUniversity of Oxford
15/04/2014
![Page 2: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/2.jpg)
About the course• In these three sessions we will cover:• Basic Programming Concepts• Perl Syntax• Data input, manipulation, and output• Provide a foundation to learn more Perl or other programming language• Provide a starting point for your own programs
• This class is designed to provide a supported learning environment
• Go at your own pace
![Page 3: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/3.jpg)
What the course is not• A complete review of Perl• An in-depth introduction to programming• A “taught” course
![Page 4: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/4.jpg)
What is Perl?• High-level. Strong abstraction from the details of the computer, some
natural language elements, easy-to-use, etc.• General-purpose. People have done almost everything imaginable
with a computer using Perl!• Interpreted. Perl translates source code into an efficient intermediate
representation which is then immediately executed.• Dynamic. Perl executes at runtime many behaviours that other low-
level languages might perform during compilation.• Free Software: Perl is available for usage without charge.
![Page 5: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/5.jpg)
A brief history of Perl• Before Perl:• C/C++/Fortran/COBOL: Compiled languages that were platform specific• Shell Scripts: Basic automation of Operating System tasks• Utilities (AWK/SED/GREP): Used to process text files
• There was a need by systems administrators to simplify things
• Perl was born in 1987
• Perl 5 (the current version) was released in 1994
![Page 6: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/6.jpg)
What is Perl used for?• Text processing. Designed as a Unix-based system for report processing
(systems administration).• Web. Perl was one of the few programming languages at the time
suitable for processing the highly textual content of the web.• Science. “Genomics” meant we needed a tool to process data involving
DNA sequences.
• Many enthusiasts have created extensions for Perl that allow its use in almost any domain but check that it really is the right language for you!
![Page 7: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/7.jpg)
Executing a Perl program• What do you need?• Source Code – a text file containing the instructions for the computer• The Perl Interpreter – perl (available at http://www.perl.org) • (Optional) Data Files
• To execute typeperl myprogram.pl“.pl” is the standard extension for a Perl program like “.doc” or “.exe”
• No executable program will be generated
![Page 8: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/8.jpg)
Alternatives to Perl• R/MATLAB/Octave/SciPy: Mathematical Computing. In Perl v5, this is
currently done via an external module (PDL) but some basic functionality will be directly incorporated into v6.
• Python. Very powerful and popular alternative to Perl.
• Compiled languages. Speed and maximum performance.
![Page 9: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/9.jpg)
Session Plan• Session 1
• Hello World! – Your first Perl program• Variables (Part 1) – Scalars and Arrays (1d and 2d)• Conditional Statements (if/else)• Loops (foreach/while)
• Session 2• File handling• Regular Expressions (RegEx)• Variables (Part 2) – Hashes
• Session 3• Functions
![Page 10: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/10.jpg)
Hello World!
• Comments – “#” Notes to the programmer from the programmer• #!/usr/bin/perl – A special message to the operating system• Often you will need to remind yourself of what is happening or you will need to explain
something to another future programmer
• Pragma – “use” Sets environmental conditions for the program• Functions – “something()” Reusable sections of code
• Sometimes the () is omitted for special functions such as “print”
• End of line – “;” The semicolon defines the end of a command/line
![Page 11: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/11.jpg)
Escape Characters• Some text mark up requires special codes• \n – New Line• \t – Tab • \\ – Normal Backslash• \” – Some times required to use double quote (use inside of strings)• \’ – Some times required to use single quote
• More information at: http://perldoc.perl.org/perlrebackslash.html
![Page 12: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/12.jpg)
use strict;
![Page 13: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/13.jpg)
Variables (Part 1) – Scalars• Numbers• Integers• Floating Point Numbers
• Strings• Chunks of text
• “this is a string”• “this is another string”• “This string has a number 182939”• “Thad said \“Hi Everyone!\””
![Page 14: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/14.jpg)
Scalars (cont.)
• To create a Scalar variable use “$”• $variable = “something”;
• To print a Scalar use the print function• print $variable;• print “$variable\n”;
![Page 15: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/15.jpg)
Numeric Operations
• Perl can perform all normal mathematical functions on number scalars• “%” – Modulo can be thought of as the remainder after a division function• “**” – Is shorthand for the exponential function• More mathematics like sin(), cos(), and tan() are available through extra libraries
![Page 16: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/16.jpg)
String Operations
• Perl is a very powerful tool for the manipulation of strings• “.” concatenates strings together• Strings surrounded in double quotes can automatically replace text for scalars in the string• String literals use single quotes and will not parse and replace anything in the string• There are a number of functions that will modify your string
![Page 17: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/17.jpg)
User Input• Scalars can be used to store information added to the program after
execution. This information can come from the user or from a file. For now we will focus on user input.
![Page 18: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/18.jpg)
Arrays
• Arrays are ordered sets of scalar variables• Arrays can mix numbers and strings in the set• Arrays use the “@” symbol when referencing them
![Page 19: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/19.jpg)
Arrays (Cont.)
• Starting with an array of 5 scalars• “shift” – takes off the first scalar• “pop” – takes off the last scalar• “push” adds a new scalar at the end of the array
![Page 20: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/20.jpg)
2D Arrays
• Members of an array can be an array• This is different to a 2D array in a language like C• These are known as “jagged arrays”• You can have higher dimensionality if needed
![Page 21: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/21.jpg)
Conditionals• Programs often need to make decisions during execution• This is handled by the ideas of if/else• if statement is true -> do something• else -> do something different
• This also introduces the idea of a Boolean true/false test• if ($name eq “Thad”) – this may or may not be true• if (9 > 0) – this is always true• if (9 < 0) – this is never true• if ($a + $b == 100) – perform the calculation then test against condition
![Page 22: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/22.jpg)
Conditionals (Cont.)
• Conditionals control the direction that the program follows• It is possible to have more than one truth statement in the if
statement
![Page 23: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/23.jpg)
Loops• Loops are used to repeat a section of code an arbitrary number of times. For
example: I want to check each employee record in a company to see if the employee’s salary is > £50,000• The classic example is the “for” loop, which starts at a number, is changed in
some way at the end of each loop and continues while a conditional statement is true.• The “for” loop was improved into the “foreach” loops which will step
through an array and offer the scalar as a variable for testing.• The final loop that we will encounter in this course is the “while” loop. The
while loop uses a conditional statement to determine if the loop should execute.
![Page 24: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/24.jpg)
For Loops• The “for” loop requires three conditions to execute• The start condition $i = 0• The conditional statement $i < 20• The step statement $i += 1
![Page 25: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/25.jpg)
Foreach Loops• Given a range or an array of items, the “foreach” loop will step
through each item and offer a special scalar $_ containing the current item.
![Page 26: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/26.jpg)
Foreach (Cont.)• Alternately to using $_ you can name the scalar to reduce confusion.
![Page 27: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/27.jpg)
While Loops• The “while” loop checks a conditional statement before executing its
code, once the end of the block has been reached it returns to the top of the code block and checks the conditional statement again.
![Page 28: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/28.jpg)
Additional Help• When in doubt use Google or other search engine. There are millions
of code examples, questions, and solutions on the web and there is no reason not to use them (with attribution if appropriate)• If you have a question that you can’t solve with Google then try asking
the community at Stack Overflow• Or find a Perl community on the web, there are a great many places
that a new programmer can go for help
![Page 29: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/29.jpg)
Practical Session• Please make your way to the computers, you will need to set up your
keyboards and the instructions are given in your course booklet.• Please feel free to ask questions• Go at your own pace• If you don’t finish in class: Perl is available for free at
http://www.perl.org and is a simple install to get on to Windows• Linux and Mac should already have Perl included• Please feel free to email me questions during the week:
![Page 30: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/30.jpg)
Introduction to PerlPart 2
Thaddeus AidIT Learning Programme
University of Oxford
![Page 31: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/31.jpg)
Review• Any questions from last week?
![Page 32: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/32.jpg)
Review• We have covered
• Hello World!• Variables
• $scalars• @arrays
• Conditional Statements• if• elsif• else
• Loops• for• foreach• while
![Page 33: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/33.jpg)
This Session• Basic File Handling• File Reading (open, <)• File Writing (open, >)
• Regular Expressions• Text searching and manipulation
• Variables (Part 2)• %hashes
![Page 34: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/34.jpg)
Reading a File• We will only be dealing with simple text file handling.• We will be using the open function and a few more in the book.• Redirection symbols are a legacy symbol from the Unix command line• < redirect input from a file• > redirect output to a file• >> redirect output to a file in append mode
• open(FILEHANDLE, “Redirection Symbol”, “Filename”);• open(INPUT, “<“, “somedata.txt”);
![Page 35: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/35.jpg)
Reading a File (Cont.)
• Reading the entire file into an array for processing.
![Page 36: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/36.jpg)
Reading a File (Cont.)
• Reading a file one line at a time.
![Page 37: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/37.jpg)
Reading a File (Cont.)
• A more advanced example• Skipping header lines• Changing carriage return for new line• Splitting a line of input
![Page 38: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/38.jpg)
Bringing it all together
• As a recommendation, never write to the file that you have read from.
![Page 39: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/39.jpg)
Regular Expressions• Regular Expressions are a very powerful way to manipulate text files.• Match – Find a substring in your string• Translate – Replace one set of letters with another• Substitute – A more powerful replacement command
![Page 40: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/40.jpg)
Matching
![Page 41: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/41.jpg)
Matching (Cont.)
• A simple example of matching
![Page 42: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/42.jpg)
Matching (Cont.)
• A more complex matching example
![Page 43: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/43.jpg)
Substitutions
• A simple substitution example
![Page 44: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/44.jpg)
Variables: Part 2 - Hashes• Hashes (known as associative arrays or maps in other languages) is
the third basic data structure in Perl.• We store data in a hash using a key and value pair.• The key acts as an index into the hash where a value is stored.• In Perl, hashes are specified using the % symbol.
![Page 45: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/45.jpg)
Hashes (Cont.)
• Hashes are like an array but use a “key” instead of an “index”.
![Page 46: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/46.jpg)
Hashes (Cont.)• An example of a hash table.• Getting all the keys can be done with
the “keys” function.• You can loop through the keys array
like any other array.• Hash keys are not sorted, because of
this.
![Page 47: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/47.jpg)
Hashes (Cont.)• “exists” checks to
see if entry is set.• “sort” will sort an
array.
![Page 48: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/48.jpg)
A Real World Example• This is a program that I wrote
to translate 22 file containing phylogenetic information about humans from one version of the genome to another version of the genome (hg18 -> hg19)
![Page 49: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/49.jpg)
Exercises• Please do the exercises in Chapters 3-5.• Go at your own pace.• Please ask questions if you get stuck.
![Page 50: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/50.jpg)
Introduction to PerlPart 3
Thaddeus AidIT Learning Programme
University of Oxford
![Page 51: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/51.jpg)
Review• Any questions from last week?
![Page 52: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/52.jpg)
Review• We have covered• Hello World!• Variables• Conditional Statements• Loops• File Handling
• Read• Write
• Regular Expressions• Variables
• Hashes
![Page 53: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/53.jpg)
This Session• Subroutines• Discussions on the projects that you are going to work on
![Page 54: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/54.jpg)
Sub-routines• sub• A subroutine (function or method in other languages) is a self contained block
of code that can be called from different parts of the program.• A subroutine may take one or more variables as input• A subroutine may return one or more variables as output
![Page 55: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/55.jpg)
sub
• You have already been using built-in subroutines such as print and open• To use a subroutine you type the name and add parentheses after• Subroutines live in code blocks
![Page 56: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/56.jpg)
Scope• We have been using my as a keyword without knowing what it means.
When we use the keyword my as the declaration of our variable we are declaring that it exists in the current scope and cannot be accessed outside of that scope.• A variable can only be seen within the scope it was created in and in
any scopes created within that scope• Scope is created by using {} and placing code between them• This is called a code block• As a suggestion in each new code block indent your code an additional tab
space
![Page 57: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/57.jpg)
Scope• The variables can be seen within the scope
that it is created and sub-scopes• The green scope can see $test1• The green scope cannot see $test4
• Variables cannot be seen in higher scopes• The blue scope cannot see $test2 or 3 or 4
• Variables only exist so long as their scope exists• Once you exit your scope your variables are
no longer accessible
![Page 58: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/58.jpg)
File Scope
• Where you declare your variables is very important, in this code the variable $x can be seen anywhere in the program.
![Page 59: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/59.jpg)
Local Scope• Again, where you declare your
variable is very important. In this example the declaration of $x cannot be seen in print_x_global()
![Page 60: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/60.jpg)
Parameters• Subroutines can take information into themselves and return sets of
information.• Parameters can be passed as scalars or as references
• Passing an array normally causes it to be treated as a series of scalars• The alternative way is to pass the address of the array and then you can “dereference”
the address to access the elements• You should pass a hash by reference and then dereference the address
• You access the passed parameters by the special array @_• You output variables using the return function
![Page 61: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/61.jpg)
Passing Parameters• In this code we are passing the
subroutine a number and returning the answer.
![Page 62: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/62.jpg)
Passing Parameters• Here we pass two scalar
variables. They are joined into an array and are access like a normal array would be.
![Page 63: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/63.jpg)
Using References• A reference is create
using the \ character such as \@x• The array is the
accessed by dereferencing the reference, such as @$array_ref• To return an array
send it as a reference.
![Page 64: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/64.jpg)
References Illustrated• Depending on how you
pass your information will change how you access your information
![Page 65: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/65.jpg)
Questions?• Any questions?
![Page 66: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/66.jpg)
What next?• There are a number of more advanced tutorials online to learn more Perl. You
can start at http://www.perl.org and looks through the available resources in the Learning Perl 5 section• CPAN hosts over 25,000 code libraries to add new functions to your programs• You can look into a course on algorithms, learning how to use and implement
algorithms will make your code better• Join Stack Overflow which is a fantastic place to find answers to difficult
programming problems• Always remember that the answer to your problems are probably only a
Google search away
![Page 67: Introduction to Perl](https://reader035.vdocument.in/reader035/viewer/2022081512/56816254550346895dd29e47/html5/thumbnails/67.jpg)
Exercises• Please complete the exercises in Chapter 6• Go at your own pace• Please feel free to ask me questions about the exercises or about your
future projects• Thank you for attending this class, I really enjoy teaching it