Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.

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

A Guide to Unix Using Linux Fourth Edition
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.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
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.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
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.
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.
CSC3530 Software Technology Tutorial Two PERL Basics.
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
Guide To UNIX Using Linux Third Edition
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
Practical Extraction & Report Language PERL Joseph Beltran.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Perl Practical(?)‏ Extraction and Report Language.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 
1 Perl Syntax: control structures Learning Perl, Schwartz.
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.
Programming Languages Meeting 14 December 9/10, 2014.
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
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
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,
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.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
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
 History  Ease of use  Portability  Standard  Security & Privacy  User support  Application &Popularity Today  Ten Most Popular Programming Languages.
Scripting Languages ● Perl, Python, Unix Shell, VB, JavaScript, etc. ● Glue Languages. ● Typeless. ● Interchangeable Code and Data. ● Interpreted vs. Compiled.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl Scripting II Conditionals, Logical operators, Loops, and File handles Suzi Lewis Genome Informatics.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.
The Scripting Programming Language
PZ02CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02CX - Perl Programming Language Design and Implementation.
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.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
CSC 4630 Meeting 7 February 7, 2007.
Perl Programming Language Design and Implementation (4th Edition)
Chapter 5 - Control Structures: Part 2
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Control Structures: if Conditional
An Introduction to Perl
Control Structures: for & while Loops
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
Perl I/O Learning Objectives:
CSC 352– Unix Programming, Fall 2012
INTRODUCTION to PERL PART 1.
Karan Thaker CS 265 Section 001
Presentation transcript:

Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004

Lenwood Heath, Virginia Tech, Fall, 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, 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, Numbers Integers: 45, 974, -892, 0 Real numbers: 45.0, , , 2.5e-3 Octal: 055 Hexadecimal: 0x2d Binary: 0b101101

Lenwood Heath, Virginia Tech, Fall, 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 &&and Or ||or Not !not

Lenwood Heath, Virginia Tech, Fall, 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, 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)ea String positions start at 0

Lenwood Heath, Virginia Tech, Fall, 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, 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, Arrays An array is a list-valued variable Array identifier = 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, Simple Perl Script #!/usr/bin/perl $dotted = `ping -c 1 $dotted`; Result: cs2204]$ dot_ping www cslab vt edu PING owlstation.cs.vt.edu ( ) 56(84) bytes of data. 64 bytes from : icmp_seq=1 ttl=64 time=0.536 ms

Lenwood Heath, Virginia Tech, Fall, Numeric Functions ASCII code — ord('?') is 63 ASCII character — chr(63) is '?' Absolute value — abs(-11) is 11 Integer value — int( ) is 295 Square root — sqrt(16) is 4 Natural logarithm — log( ) is 5.69 Integer value — int( ) is 295 Random number — rand(10) was

Lenwood Heath, Virginia Tech, Fall, String Functions Length — length('Learning Perl') is 13 Find substring — indexrindex Extract substring — substr 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, Array Functions Stack; top on the right 2 3/; to 'top' to returns 'top' Stack; top on the left 7 a/; to 'a' to returns 'a' Reverse a list Reverse qw/8 l a p 7/ returns qw/7 p a l 8/

Lenwood Heath, Virginia Tech, Fall, Array Functions (Continued) Array to = qw/9b4 x.; pbj/; 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, Input Text files Sequence of lines, each terminated by a newline File access by a file handle — standard input STDIN Read a line $line = ; Read remaining = ;

Lenwood Heath, Virginia Tech, Fall, 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, if Statementt if (condition) { statements; } elsif (condition) { statements; } else { statements; } if (not defined $ARGV[0]) { die "Usage:\n\tpaint [COLOR]\n"; }

Lenwood Heath, Virginia Tech, Fall, while Statementt while (condition) { statements; } $term = $ln = 0; while ($line = ) { chomp $line; $ln++; if ($line eq $term) { print "Term $term on line $ln.\n"; break; }

Lenwood Heath, Virginia Tech, Fall, 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, foreach Statement foreach $x { # 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, Sample Script #!/usr/bin/perl $word = while ($line = ) { if (index($line,$word) > -1) { } else { } foreach $line { print $line; }

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