Introduction to Perl. Perl Background. Sept 24, 2007 Class Meeting 6

Similar documents
Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

(Refer Slide Time: 01:12)

Programming Perls* Objective: To introduce students to the perl language.

Scripting Languages Perl Basics. Course: Hebrew University

Perl. Many of these conflict with design principles of languages for teaching.

UNIX Shell Programming

Perl. Perl. Perl. Which Perl

1. Introduction. 2. Scalar Data

The Power of Perl. Perl. Perl. Change all gopher to World Wide Web in a single command

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Programming introduction part I:

What is PERL?

CSCI-GA Scripting Languages

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

COMS 3101 Programming Languages: Perl. Lecture 1

Pathologically Eclectic Rubbish Lister

Perl for Biologists. Session 2 March 19, Constants, variables and functions. Jaroslaw Pillardy

A control expression must evaluate to a value that can be interpreted as true or false.

Welcome to Research Computing Services training week! November 14-17, 2011

T-( )-MALV, Natural Language Processing The programming language Perl

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

PERL Scripting - Course Contents

COMP284 Scripting Languages Lecture 2: Perl (Part 1) Handouts

CS Unix Tools & Scripting

Perl. Interview Questions and Answers

COMS 3101 Programming Languages: Perl. Lecture 2

Introductory Perl. What is Perl?

8/13/ /printqp.php?heading=II BSc [ ], Semester III, Allied: COMPUTER PROGRAMMING-PERL -309C&qname=309C

A Crash Course in Perl5

Outline. Introduction to Perl. Why use scripting languages? What is expressiveness. Why use Java over C

Title:[ Variables Comparison Operators If Else Statements ]

Lecture 3. More About C

Examples of Using the ARGV Array. Loop Control Operators. Next Operator. Last Operator. Perl has three loop control operators.

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Introduc)on to Unix and Perl programming

211: Computer Architecture Summer 2016

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

COMP 4/6262: Programming UNIX

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Grep and Shell Programming

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Useful Unix Commands Cheat Sheet

Basic Linux (Bash) Commands

Shells and Shell Programming

Script Programming Systems Skills in C and Unix

Perl Tutorial. Diana Inkpen. School of Information Technology and Engineering University of Ottawa. CSI 5180, Fall 2004

Lecture 2: Programming in Perl: Introduction 1

CSCI-GA Scripting Languages

Beginning Perl. Mark Senn. September 11, 2007

A shell can be used in one of two ways:

Bamuengine.com. Chapter 14. Perl The Mater Manipulator

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Classnote for COMS6100

Scripting Languages. Diana Trandabăț

Introduc)on to Unix and Perl programming

Variables, Constants, and Data Types

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

The e switch allows Perl to execute Perl statements at the command line instead of from a script.

Shells and Shell Programming

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Shell Programming (bash)

Vi & Shell Scripting

Shells & Shell Programming (Part B)

Manual Shell Script Linux If Not Equal String Comparison

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Introduction to Perl programmation & one line of Perl program. BOCS Stéphanie DROC Gaëtan ARGOUT Xavier

Lecture 12. PHP. cp476 PHP

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Perl Scripting. Students Will Learn. Course Description. Duration: 4 Days. Price: $2295

A Crash Course in Perl5

Introductory Perl. Boston University Information Services & Technology. Course Coordinator: Timothy Kohl. What is Perl?

print STDERR "This is a debugging message.\n";

CMSC 331 Final Exam Fall 2013

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

PERL Bioinformatics. Nicholas E. Navin, Ph.D. Department of Genetics Department of Bioinformatics. TA: Dr. Yong Wang

Essentials for Scientific Computing: Bash Shell Scripting Day 3

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

The Design of C: A Rational Reconstruction (cont.)

What is Bash Shell Scripting?

9.1 Origins and Uses of Perl

CS 230 Programming Languages

Regular expressions and case insensitivity

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CMPT 125: Lecture 3 Data and Expressions

Introduction to CFX. Appendix A. Scripting and Automation A-1. ANSYS, Inc. Proprietary 2009 ANSYS, Inc. All rights reserved.

CMSC 330: Organization of Programming Languages. Ruby Regular Expressions

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Control Structures. CIS 118 Intro to LINUX

More Perl. CS174 Chris Pollett Oct 25, 2006.

COMP284 Scripting Languages Lecture 10: PHP (Part 2) Handouts

Regular expressions and case insensitivity

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

DEMO A Language for Practice Implementation Comp 506, Spring 2018

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Transcription:

Introduction to Perl Sept 24, 2007 Class Meeting 6 * Notes on Perl by Lenwood Heath, Virginia Tech 2004 Perl Background Practical Extraction and Report Language (Perl) Created by Larry Wall, mid-1980's Language combining capabilities of shell programming, awk, grep, lex, sed, and a number of other UNIX utilities Powerful, complex scripting language We learn just a bit! Lenwood Heath, Virginia Tech, Fall, 2004 2 1

Scalars Basic data type in Perl is scalar Most scalar values are numbers or character strings Programmer forces interpretation of a scalar value as a number or string by operations used Special scalar value undef is neither number nor string, just "undefined" Lenwood Heath, Virginia Tech, Fall, 2004 3 Numbers Integers: 45, 974, -892, 0 Real numbers: 45.0, 10.237, -101.1, 2.5e-3 Octal: 055 Hexadecimal: 0x2d Binary: 0b101101 Lenwood Heath, Virginia Tech, Fall, 2004 4 2

Numeric Operators Arithmetic: 4+5, 9-7, -9*-3, 10/3 Modulus (remainder): 102 % 9 is 3 Comparisons: <, >, <=, >=, ==,!= Spaceship: <=> (-1, 0, or 1) Logical And Or && and or Not! not Lenwood Heath, Virginia Tech, Fall, 2004 5 Strings Zero or more characters: "", "one" No concept of null-termination Single (literal) quotes 'tab\tnl\n' tab\tnl\n 9 Double (interpreted) quotes "tab\tnl\n" tab_nl_ 7 Escaped double quote "Here's a double quote \"." Lenwood Heath, Virginia Tech, Fall, 2004 6 3

String Operators Concatenation: "Learning "."Perl" Comparisons: lt, gt, le, ge, eq, ne Index: position of a substring in a string index('learning Perl','rni') 3 index("learning Perl",'nr') -1 Substring: select a substring substr('learning Perl',1,2) String positions start at 0 ea Lenwood Heath, Virginia Tech, Fall, 2004 7 Scalar Variables Scalar variable identifier begins with $ $colors = "red green blue"; $count = $count+1; Shortcuts and alternatives: $colors.= ' purple'; $count += 1; $count++; Interpolation: "Count is $count.\n" Lenwood Heath, Virginia Tech, Fall, 2004 8 4

Lists Sequence of scalars (5.7,"house\tbarn",'-9.2') qw shortcut equivalent lists: ("VT","UNC","NCSU","UVa","Wake") qw/ VT UNC NCSU UVa Wake / qw{ VT UNC NCSU UVa Wake Lenwood Heath, Virginia Tech, Fall, 2004 9 Arrays An array is a list-valued variable Array identifier begins with @ @colors = qw(red green blue); Array element reference: $id[index] $colors[2] # Value is 'blue' $colors[8] = 'purple'; substr($colors[1],0,3)# 'gre' Lenwood Heath, Virginia Tech, Fall, 2004 10 5

Simple Perl Script #!/usr/bin/perl $dotted = join('.',@argv); @ping = `ping -c 1 $dotted`; print @ping[0..1]; Result: [cs2204@peach cs2204]$ dot_ping www cslab vt edu PING owlstation.cs.vt.edu (128.173.40.52) 56(84) bytes of data. 64 bytes from 128.173.40.52: icmp_seq=1 ttl=64 time=0.536 ms Lenwood Heath, Virginia Tech, Fall, 2004 11 Numeric Functions ASCII code ord('?') is 63 ASCII character chr(63) is '?' Absolute value abs(-11) is 11 Integer value int(295.143) is 295 Square root sqrt(16) is 4 Natural logarithm log(295.143) is 5.69 Integer value int(295.143) is 295 Random number rand(10) was 4.94028 Lenwood Heath, Virginia Tech, Fall, 2004 12 6

String Functions Length length('learning Perl') is 13 Find substring index Extract substring substr rindex Lower case lc('9jp.ir') is '9jp.ir' Upper case uc('9jp.ir') is '9JP.IR' Remove last character chop($word); Remove newline at end chomp($line); Lenwood Heath, Virginia Tech, Fall, 2004 13 Array Functions Stack; top on the right @stack=qw/1 2 3/; push(@stack,'top') updates @stack to 1 2 3 'top' pop(@stack) updates @stack to 1 2 3 returns 'top' Stack; top on the left @stack=qw/4 7 a/; unshift(@stack,8) updates @stack to 8 4 7 'a' shift(@stack) updates @stack to 8 4 7 returns 'a' Reverse a list Reverse qw/8 l a p 7/ returns qw/7 p a l 8/ Lenwood Heath, Virginia Tech, Fall, 2004 14 7

Array Functions (Continued) Array to string @words = qw/9b4 x.; pbj/; $words=join('--',@words); is '9b4--x.;--pbj' String to array split(/b/,$words) is qw/9 4--x.;--p j/ Sorting lexicographic order sort qw/red green blue/ returns qw/blue green red/ Sorting numerical order sort { $a <=> $b (94,-1,55) returns (-1,55,94) Lenwood Heath, Virginia Tech, Fall, 2004 15 Input Text files Sequence of lines, each terminated by a newline File access by a file handle standard input STDIN Read a line $line = <STDIN>; Read remaining lines @lines = <STDIN>; Lenwood Heath, Virginia Tech, Fall, 2004 16 8

Output Standard output STDOUT print 'A line to', " standard output\n"; Standard error STDERR print STDERR "Arguments OK\n"; warn "Unable to find config file.\n"; die "Unexpected system error"; Lenwood Heath, Virginia Tech, Fall, 2004 17 if Statement if (condition) { statements; elsif (condition) { statements; else { statements; if (not defined $ARGV[0]) { die "Usage:\n\tpaint [COLOR]\n"; Lenwood Heath, Virginia Tech, Fall, 2004 18 9

while Statement while (condition) { statements; $term = shift(@argv); $ln = 0; while ($line = <STDIN>) { chomp $line; $ln++; if ($line eq $term) { print "Term $term on line $ln.\n"; break; Lenwood Heath, Virginia Tech, Fall, 2004 19 for Statement for (initialization; test; increment) { statements; for ($i = 0; $i < length($line); $i++) { if (lc($char) eq substr($line,$i,1)) { print "Character $char found.\n"; last; elsif (uc($char) eq substr($line,$i,1)) { print "Character $char found.\n"; last; Lenwood Heath, Virginia Tech, Fall, 2004 20 10

foreach Statement foreach $x (@y) { # execute for each element of list statements; foreach $color (qw/red green blue purple/) { print "$color is a color!\n"; Lenwood Heath, Virginia Tech, Fall, 2004 21 Sample Script #!/usr/bin/perl $word = shift(@argv); while ($line = <STDIN>) { if (index($line,$word) > -1) { unshift(@contains,$line); else { unshift(@lacks,$line); foreach $line (@CONTAINS) { print $line; Lenwood Heath, Virginia Tech, Fall, 2004 22 11

Topics for Next Lecture Subroutines Regular expressions Hashes File input/output File tests Invoking UNIX commands Lenwood Heath, Virginia Tech, Fall, 2004 23 12