PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
A Guide to Unix Using Linux Fourth Edition
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
● Perl reference
CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
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.
Introduction to Perl Bioinformatics. What is Perl? Practical Extraction and Report Language A scripting language Components an interpreter scripts: text.
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.
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.
Practical Extraction & Report Language Picture taken from
CSC3530 Software Technology Tutorial Two PERL Basics.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
Guide To UNIX Using Linux Third Edition
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
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.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Practical Extraction & Report Language PERL Joseph Beltran.
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.
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue.
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.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
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.
Programming Languages Meeting 14 December 9/10, 2014.
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
5 1 Data Files CGI/Perl Programming By Diane Zak.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
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.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
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.
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
The Scripting Programming Language
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.
Organization of Programming Languages Meeting 37 April 18, 2016.
Perl Programming Language Design and Implementation (4th Edition)
Perl Variables: Array Web Programming.
Control Structures: for & while Loops
Pemrosesan Teks Herika Hayurani Sept 19, 2006
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 Languages
Shell Control Structures
Chapter 5 Bourne Shells Scripts
The Selection Structure
INTRODUCTION to PERL PART 1.
Presentation transcript:

PERL By C. Shing ITEC Dept Radford University

Objectives Understand the history Understand constants and variables Understand operators Understand control structures Understand functions Understand file I/O Understand command line arguments Understand how to use Perl for system administration

History Perl (Practical Extraction Report Language) by Larry Wall (1986) Language between C and shell: Scripting, need interpreter Provide File access and Unix utilities Download site: –File extension (.pl) –Interpret with error message: perl -w filename.pl arg0 arg1 … Artistic license: variation of GNU public license, free

Constants Scalar: –Number: integer, real, scientific notation –String: within “ “ Boolean: –1 represents true –0 represents false

Variables Scalar: begin with $ –Number: integer, real –String: string, character Array: begin –Index starts from 0 Associative: begin with % –use hash

Control Structures Print Assignment Conditional Loop Function (Subroutine)

Control Structures - Print Syntax: each statement ends with ; Print variable [constant]; –Example: print "Please enter a number:";

Control Structures - Assignment Syntax: each statement ends with ; Variable = expression Comment: begins with #

Control Structures - Assignment Scalar Variable Number: integer, real –Example: $i=5 $j=-6.2 String: string, character –Example: $month=“January” $char=“\t”

Control Structures - Assignment Scalar Variable Number: integer, real –Example: $i=5; $j=-6.2; $number = ; (keyboard input) String: string, character –Example: $month=“January”; $char=“\t”; $guess = ; (keyboard input) $fileString = (getting data from file)

Control Structures - Assignment Array Variable = ("john", "susan", "david", "sarah", "ryan", = (1..6); Where $array[0] = “john”; $array[1] = “susan”; $array[5] = “laura”;

Control Structures - Assignment Associative Variable –Example: %hash = ("smith"=>"john", "sides"=> "susan", "davis"=>"david", "star"=>"sarah", "rush"=>"ryan","lane"=> "laura"); Where $array{“smith”} = “john”; $array{“sides”} = “susan”; $array{“lane”} = “laura”;

Operators – Arithmetic, Logic, Relational Arithmetic: +,-,*,/ Logical: &&, || Relational –Numeric: ==, !=, >, >=, <, <= –String: eq, ne, gt, ge, lt, le

Operators - String String –Concatenation:. Example: –$month = $month.$char; –$message = "This is a number ".”\t”.$number. "\n"; –Match: =~ Example; $line =~ s/the/THE/g; (replace all the by THE in the $line) $line =~ /(\w+)(\s+)(\w+)(\s+)/; (line that matches the pattern: word, spaces, word

Control Structures - Conditional Syntax 1.If (condition) { } 2.If (condition) { } else { } 3.If (condition1) { } elsif (condition2) { } else { }

Control Structures - Conditional Example: if ($guess eq "hello") { print "The word ".$guess." that you guessed is correct.\n\n"; }

Control Structures - Conditional Example: if ($avg >=0.895*$avgall) { $grade="A"; } elsif ($avg >=0.795*$avgall) { $grade="B"; } elsif ($avg >=0.695*$avgall) { $grade="C"; } elsif ($avg >=0.595*$avgall) { $grade="D"; } else { $grade="F"; }

Control Structures - Loop 1.While 2.Until 3.Foreach 4.For

Control Structures – While Loop Syntax while (condition) { } when condition is false, exit loop

Control Structures – While Loop while ($line= ) { }

Control Structures – Until Loop Syntax until (condition) { } when condition is true, exit loop

Control Structures – Until Loop Example: until ($array2[0] eq "listing") { }

Control Structures – Foreach Loop Syntax foreach variable (variableRange) { } when variable is outside of variableRange, exit loop

Control Structures – Foreach Loop Example: foreach $month (1..12) { }

Control Structures – For Loop Syntax for (initialization; condition; ending body stmt) { } when condition is false, exit loop

Control Structures – For Loop Example: for ($i=1; $i<=12; ++$i) { }

Control Structures - Function (Subroutine) 1.Library function 2.User (-defined) function

Control Structures – Library Function 1.lc() or lc : convert to lower case 2.uc(): convert to upper case 3.open(), close(), die(), exit(), last(): See File I/O 4.print() 5.length(): length of string 6.index()

Control Structures – Library Function Example: # search item through list foreach $user { if ($user eq lc($item)) { $user_correct = 1; } } # foreach

Control Structures – Library Function 7.keys(): gives the key value of the hash 8.chomp(): delete the new line character from variable 9.split (delimiter, variable) : split a variable using delimiter 10.my(): create local variable 11.shift (arrayName): get rid of 1 st element in the arrayName 12.push (array1, array2): push array2 to the end of array1

Control Structures – Library Function Example: while ($line= ) { chomp($line); # username are separated by /\s+/, $line; }

Control Structures – Library Function Example: foreach $userkey (keys %hash) { if ($hash{$userkey} eq lc($name)) { $user_correct = 1; $lastname=$userkey; } } # foreach

Control Structures – User Function Function call: –Syntax: functionname (actualParameterList); –Example: check

Control Structures – User Function Function definition: –Syntax: sub functionname { my $localVar1 = $_[0]; my $localVar2 = $_[1]; }

Control Structures – User Function Function definition: –Example: check sub check { # the following 3 lines declare local variables my $item = $_[0]; my $user_correct = 0; }

Control Structures – User Function Example: sub printArray { is a local variable in the sub foreach $cell { print $cell."\t"; } print "\n"; } # sub printArray

File I/O Open/Close file: –Syntax: open (FILEHANDLE, “<filename”); close (FILEHANDLE);

File I/O Open/Close file: –Example: # read all names from input data file: sixth2.txt open (NAMEFILE, "<sixth2.txt") || die "Error: Unable to open the file $!"; # open output file for print open (OUTFILE,">attend.txt"); print OUTFILE "Lastname\t\tFirstname\t\t Name\n";

Command Line Arguments perl –w filename.pl arg0 arg1 arg2 … Arg0, arg1 and arg2… are command line arguments Inside the program, they are stored in

Command Line Arguments Example: open (USERFILE, "<user$ARGV[0].txt") || die "Error: Unable to open the file $!"; open (OUTFILE,">attend$ARGV[0].txt"); print OUTFILE "Library Attendance for ITEC 100-$ARGV[0]\n\n";

System Administration Call system library when using shell commands in Perl Give full path name for shell commands –Use which command to find full path name Watch for reserved names in Perl

System Administration Example: Run Perl in Unix environment –system("/usr/local/gnu/bin/awk '{print \$1,\$2,\$9}' temp1.txt > temp2.txt"); –system("/usr/bin/rm -f temp2.txt"); If you run Perl in Unix, make sure to change data into Unix format, such as: –system("/usr/bin/dos2unix session.txt session.txt");

Reference Some simple examples