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.

Slides:



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

C Language.
A Guide to Unix Using Linux Fourth Edition
● Perl reference
The Web Warrior Guide to Web Design Technologies
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
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.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
Practical Extraction & Report Language Picture taken from
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
CSC3530 Software Technology Tutorial Two PERL Basics.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to Perl Thaddeus Aid IT Learning Programme University of Oxford 15/04/2014.
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:
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Chapter 3: Data Types and Operators JavaScript - Introductory.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Introduction to Perl Yupu Liang cbio at MSKCC
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
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 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.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
5 1 Data Files CGI/Perl Programming By Diane Zak.
Writing Scripts Hadi Otrok COEN 346.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
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.
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.
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.
The Scripting Programming Language
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Perl Subroutines User Input Perl on linux Forks and Pipes.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Linux Administration Working with the BASH Shell.
Programming Basics Web Programming.
Variables, Expressions, and IO
Perl for Bioinformatics
Control Structures: for & while Loops
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
PHP an introduction.
INTRODUCTION to PERL PART 1.
Presentation transcript:

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 but lot easier and more powerful. Perl is free to download and is also available for Windows and Macintosh. File name extension.pl The first line of a perl program should tell where to find the perl intepreter #!/usr/bin/perl

Steps to Run a Perl Program Use your favorite editor to create a Perl program, say test.pl. Change the file permission and make it executable. chmod 700 test.pl Run Perl program test.pl

Comments The pound sign "#" is the symbol for comment entry. Exception: First line, #!/usr/bin/perl, tells where to find the Perl compiler on your system

Variables Three types of variables. Scalar Array Hash

Scalars Scalar means single value In C/C++, many different kinds of single values: int, float, double, char, bool In Perl, Scalar variable can hold all these types, and more, such as string. In Perl, we do not need to declare the data type of variables

Scalars All Scalar variables begin with a $ Examples: $foo, $a, $zebra1, $F87

Scalar Assignments Scalars hold any data type: $foo = 3; $d = 4.43; $temp = ‘Z’; #could be double quote $My_String = “Hello, I’m Paul.” # could be single quote $value = TRUE;

Arithmetic in Perl 9 $a = 1 + 2;# Addition $b = 3 - 4; # Subtraction $c = $a * $b; # Multiplication $d = $a / $b; # Division $a = 9 ** 10; # Exponentiation $b = 5 % 2; # Modulo

Single and double quotes 10 $a = 'apples'; # you can also use double quote $b = 'bananas'; # you can also use double quote print '$a and $b'; display: $a and $b print "$a and $b"; display: apples and bananas Single quotation marks do not interpret, and double quotation marks do

Arrays Concept is the same as in C/C++ Groups of other values much more dynamic than C/C++ no declaration of size, type can hold any kinds of value, and multiple kinds of values All array variables start with @temp34 Array index stars from 0

Array = (3, 2, 1, = (34, ‘z’, “Hi”, 43.2); Assignment $a2[2] = ‘X’; (34, ‘z’, ‘X’, 43.2) Copy (3, 2, 1, 4); Merge (3, 2, 1, 4, 34, ‘z’, “Hi”, 43.2) How about this (3, 2, 1, 4, 34, ‘z’, “Hi”, 43.2)

Sort = (3, 2, 1, 4); Sort = sort (1, 2, 3, 4) How about this = (3, 22, 11, = sort (11, 22, 3, 4) This is same = sort {$a cmp $b Array is sorted alphabetically (elements are considered as string)

Sort Array (continued) Sorted = (3, 22, 11, = sort ; = sort {$a cmp $b (11, 22, 3, 4) Sort = (3, 22, 111, = sort {$a $b (3, 4, 11, 22) Note: Inside sort block, two variables must be $a and $b If $a and $b is exchanged, the sorting order is changed

More about arrays special variable for each = (3, 25, 43, 31); $#foo (a variable: last index which is 3). $#foo+1 (size of which is 4).

Program Flow: if statement if ($x == $y) { #... } elsif ($x == ($y+1)) { #... } else { #... }

Program Flow: Comparing variables Numbers == != < > Strings eq ne lt gt

Program Flow: Logical operators  && (logical and)  ||(or)  !(negation)

Program Flow (Loops) for ($t = 0; $t < 100; $t++) { #... } while ($x == $y) { #... }

foreach Statement This statement takes an array variable and assigns one item at a time to a scalar variable, executing a block of code. For an array foreach $var { # }

Basic IO Output to terminal The print statement. Example: print “My name is $name\n”; Input from keyboard The <> operator Example: $input = <>; read one line from keyboard, and save in variable $input

Task 1 Write a Perl program to ask the user to enter a name, then it will display Hello name_user_rntered

Perl Program for Task 1 #!/usr/bin/perl print "Enter your name:"; $name = <>; print "Hello $name\n";

Chomp When reading in, carriage return (“\n”) is included. Usually don’t want that. chomp will take off the last character of a string, if it is a “\n”. chomp ($foo);

Perl Program for Task 1 (revised) #!/usr/bin/perl print "Enter your name:"; $name = <>; chomp($name); print "Hello $name\n";

Read / Write to Files To read and write to files we should create something called handles which refer to the files.

Read from Files To create a file handle for reading Use the OPEN command Example open(filehandle1,"filename1");

Read from Files (continued) Once the file handles have been obtained, we can read data (line by line) from the file. = ; This will result in each line being read from the file pointed by the file handle and all lines are stored in the array where index 0 ($lines[0]) contains first line of the file, index 1 ($lines[1]) contains second line of the file, and so on.

Read from Files (continued) After read file, we should close the file. close(filehandle1);

Task 2 Write a Perl program that can read a file (test.cpp) and display each line with a line number

Perl program for Task 2 #!/usr/bin/perl open(fh1, ; close(fh1); $i=1; foreach { print "$i: $line\n"; $i=$i+1; }

Write to Files To create a file handle for writing Use the OPEN command Example open(filehandle2, ">filename2");

Write to Files (continued) Once the file handles have been obtained, we can write data to the file. Example: print filehandle2 "$linevalue"; This will result in the value of $linevalue being written to the file pointed by the filehandle2.

Write to Files (continued) After write to file, we should close the file. close(filehandle2);

Task 3 Rewrite the Perl program for Task 2 so that the result will be write to a file (test_c.cpp) instead of displaying on the screen.

Perl program for Task 3 #!/usr/bin/perl open(fh1, ; close(fh1); $i=1; open(fh2, ">test_c.cpp"); foreach { print fh2 "$i: $line\n"; $i=$i+1; } close(fh2);

Subroutines (functions) To define your own subroutine, use the keyword sub Can be defined anywhere in your program sub function_name { #commands }

Function Calls $Name = getname(); #return a value getname(); #not returning a value

Parameters of Functions Parameters are passed in a function as an array. The parameter is taken in as an array which is denoted inside the function. So if you pass only one parameter the size of will be one. If you pass two parameters then size will be two and the two parameters can be accessed by $_[0],$_[1], and so on.

Subroutines #!/usr/bin/perl $result = max(11, 12); Print “The largest number is: $result \n”; sub max { if($_[0] > $_[1]) { return $_[0]; } else { return $_[1]; } Output: The largest number is: 12

More About Functions The variables declared in the main program are by default global so they will continue to have their values in the function also. Local variables are declared by putting my key word while declaring the variable.

Subroutines: local variable example #!/usr/bin/perl sub max { if($num[0] > $num[1]){ return $num[0]; } else{ return $num[1]; } $result = max(11, 12); Print “The largest number is: $result \n”;

A routine (user defined) to read web pages sub getweb { my $url = $_[0]; require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get($url); return $response->content; } This routine takes one parameter (a web address) and returns the contents of a web page as one string

Task 4 Display the html code of

Perl program for task 4 #!/usr/bin/perl $google = getweb(" print $google;#the entire page is saved as one string sub getweb { my $url = $_[0]; require LWP::UserAgent; my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get($url); return $response->content; }

Reading Assignment Textbook: Chapter 11