Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.

Slides:



Advertisements
Similar presentations
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
Advertisements

● Perl reference
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Bioinformatics Lecture 7: Introduction to Perl. Introduction Basic concepts in Perl syntax: – variables, strings, input and output – Conditional and iteration.
Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall; ISBN , 302 pages. Second Edition, July 1997.
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.
CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
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.
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.
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.
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.
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.
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,
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.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
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.
Perl Lab #11 Intro to Perl Debbie Bartlett. Perl Lab #1 2 Perl Practical Extraction and Report Language –or- Pathologically Eclectic Rubbish Lister Created.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
The Scripting Programming Language
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Organization of Programming Languages Meeting 37 April 18, 2016.
Perl Programming Language Design and Implementation (4th Edition)
An Introduction to Perl – Part I
Perl Variables: Array Web Programming.
Control Structures: if Conditional
Introduction to Perl Jarrad Battaglia.
Perl Variables: Hash Web Programming.
An Introduction to Perl
Control Structures: for & while Loops
Lesson 2. Control structures File IO - reading and writing Subroutines
The Selection Structure
INTRODUCTION to PERL PART 1.
Karan Thaker CS 265 Section 001
Presentation transcript:

Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard Things Possible Perl is a language for easily manipulating text, files, and processes Combines concepts from unix, sed, awk, shell scripts Language of system administrators, web developers and more… Practical Extraction and Report Language Pathologically Eclectic Rubbish Lister *Many of the examples in this lecture come from “Learning Perl”, 3rd Ed, R. Schwartz & T. Phoenix, O’Reilly, 2001

Topics Getting Started scalars lists and arrays hashes I/O File handles regular expressions

Hello, World! #!/usr/bin/perl print "Hello, World!\n"; @lines = `perldoc -u -f atan2`; foreach (@lines) { s/(\w)<([^>]+)>/$1<\U$2>/g; print; }

A More Complicated Example #!/usr/bin/perl -w open(FIND,"find . -print |") || die "Couldn't run find: $!\n"; FILE: while ($filename = <FIND>) { chomp($filename); next FILE unless -T $filename; if (!open(TEXTFILE, $filename)) { print STDERR "Can't open $filename--continuing...\n"; next FILE; } while (<TEXTFILE>) { foreach $word (@ARGV) { if (index($_,$word) >= 0) { print "$filename: $word\n"; next;

Getting Help man perl perldoc Learning Perl Programming Perl www.cpan.org, www.pm.org, www.perlmonks.org

scalars numbers: 3, 3.14159, 7.24e15 strings: ‘fred’, “barney”, “hello\n” variables: $name, $count assignment: $name = ‘fred’; $count = 1; $count += 1; $name = $fred . ‘flinstone’; special variables: $_

operators numbers strings 2+3, 5.1-2.4, 3 * 12, 14/2, 10/3 ==, !=, <, >, <=, >= strings concatenation: str1 . str2 replication: str x num eq, ne, lt, gt, le, ge

print single vs. double quotes print “My name is $name\n” ‘$firstname flinstone’ “$firstname flinstone” print “My name is $name\n” print $firstname, $lastname, “\n” STDOUT, STDERR

Conditionals Boolean value (any scalar value) $count = 10; false: undef, 0, ‘’, ‘0’ true: everything else $count = 10; if ($count > 0) { print $count, “\n”; $count -= 1; } else { print “blast off\n”; }

Loops $count =10; while ($count > 0) { print $count, “\n”; } print “blast off\n”;

Getting User Input line input operator: <STDIN> $line = <STDIN> # includes \n chomp removes \n chomp($line)

Sum Odd Numbers #!/usr/bin/perl #Add up some odd numbers $n = 1; print "How many odd numbers do you want to add? "; $howmany = <STDIN>; chomp($howmany); while ($n <= $howmany) { $sum += 2*$n - 1; $n += 1; } print "The sum of the first $howmany odd numbers = $sum\n";

Exercise 2.1 Write a program that computes the circumference of a circle with radius 12.5. Use $pi = 3.141592654

Exercise 2.2 Modify the previous program to prompt and read the radius

Exercise 2.3 Modify the previous program so that if the radius is less than zero, the circumference is set to zero.

Exercise 2.4 Write a program that prompts for and reads two numbers, on separate lines, and prints their product.

Exercise 2.5 Write a program that prompts for and reads a string and a number (on separate lines) and prints the string the number of times indicated by the number (on separate lines).

Arrays and Lists Used interchangeably List variables @name List literals (“fred”,2,3) @primes = (2,3,5,7,11,13,17) @range = 1..10 Accessing elements: $primes[3] Length of a list: $#primes List assignment: ($p1, $p2, $p3) = (2,3,5)

List Operators @array = 1..5; The pop operator removes the last element of a list $last = pop(@array); @array = (1,2,3,4); $last=5 The push operator appends an element to the end of a list push(@array,5); @array = (1,2,3,4,5)

List Operators @array = 1..5; The shift operator removes the first element of a list $first = shift(@array); $first = 1; @array = (2,3,4,5) The unshift operator prepends an element to the beginning of a list unshift(@array,1); @array = (1,2,3,4,5)

List Operators @array = 1..5; The reverse operator reverses the elements of a list @rarray = reverse(@array); The sort operator sorts the elements of a list @sarray = sort(@rarray); @students = (“Sam”, “Fred”, “Anna”, “Sue”); print sort(@students);

foreach Control Structure foreach $i (1..10) { print “$i\n”; } foreach (1..10) { print “$_\n”;

Reading Lines #!/usr/bin/perl chomp(@lines = <STDIN>); # read lines, not newlines foreach $line (@lines) { print "$line\n"; }

Exercise 3.1 Write a program that reads a list of strings on separate lines until end-of-input and prints the list in reverse order.

Exercise 3.2 Write a program that reads a list of numbers on separate lines until end-of-input and then prints for each number the corresponding person’s name from the list fred betty barney dino wilma pebbles bamm-bamm

Exercise 3.3 Write a program that reads a list of strings on separate lines until the end-of-input. Then it should print the strings in alphabetical order.

Hashes An array that can be indexed by arbitrary strings $family_name{“fred”} = “flinstone; $family_name{“barney”} = “rubble”; foreach $person in ($barney, $fred) { print “Full name = $family_name{$person}\n”; }

Hashes The hash as a whole is referred to by a variable whose name starts with % %hash = (“barney”, “rubble”, “fred”, “flinstone”); %hash =(“barney” => “rubble”, “fred” => “flinstone”); @key-value-list = %hash

Hashes To obtain the keys in a hash To obtain the values in a hash @first_names = keys(%hash); To obtain the values in a hash @last_names = values(%hash);

The each Function You can loop over the key-value pairs in a hash while ( ($key, $value) = each %hash ) { print “$key => $value\n”; } The order is not specified – use sort if you care. foreach $key (sort keys %hash) { $value = $hash{$key};

The exists Function You can query to see if an entry with a given key has been inserted into a hash if (exists $last_name{$person}) { print “$person has a last name\n”; }

Deleting Entries from a Hash delete($family_name{fred});

Exercise 5.1 Write a program that will ask the user for a given name and report the corresponding family name.

Exercise 5.2 Write a program that reads a series of words (with one word per line) until end-of-input, then prints a summary of how many times each word was seen.