CSI605 perl. Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl.

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
● 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.
CS 330 Programming Languages 09 / 27 / 2007 Instructor: Michael Eckmann.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
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.
Where to get Perl ? / Unix/Linux/Solaris Unix/Linux/Solaris Interpreter (called “perl”) comes pre-installed usually Interpreter.
Perl Basics Chapters 1-6 of “Learning Perl” By Randal Schwartz, Tom Christiansen & Larry Wall; ISBN , 302 pages. Second Edition, July 1997.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
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.
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.
COMP2004 Programming Practice Sam Holden Department of Computer Science University of Sydney.
Perl data, scalar data types and control structures.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
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.
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue.
Print 'Hello world.'; Tren Griffith. Outline:  Perl introduction  Scalar Data  Variables  Operators  Control Structures  Input  Lists and Arrays.
1 Introduction to Scripting Languages and Perl Basics CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos.
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
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.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
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.
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.
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.
Introduction to Unix – CS 21
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”
Introduction to Perl William G. Dishman CUR/516 November 5, 2014.
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.
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
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.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
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.
5.1 Revision: Ifs and Loops. 5.2 if, elsif, else It’s convenient to test several conditions in one if structure: print "Please enter your grades average:\n";
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.
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.
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.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Control Structures: if Conditional
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.
Perl Programming Dr Claire Lambert
INTRODUCTION to PERL PART 1.
Karan Thaker CS 265 Section 001
Presentation transcript:

CSI605 perl

Perl Facts Perl = Pathologically Eclectic Rubbish Lister Perl is highly portable across many different platforms and operating systems Perl is really designed to digest large amounts of data Perl is not a strictly interpreted language in that Perl first compiles your program into an intermediate program Perl community has a rich history of being highly helpful Two handy webpages Perl behaves as a natural language processor

Perl Hello World Example print "hello, world \n"; To compile and run this program save it as helloworl.pl perl helloworld.pl perl -w helloworld.pl Includes warnings

Scalers $x = 34; $pi = 3.14; $avogadros = 6.02e23; $name = “Jeff”; $procalmation = “I love $name”; $y = $x; $a = $pi * $r * $r; $cwd = `pwd`string ouutput from a command $exit = system(“vi $x”);

Scaler Behavior Undeclared variables are initialized to either “ “ or 0 We can do things like this x = ‘23’; print $x + 3,”\n”;

Arrays Here is one way to assign an = {“curley”, “moe”, “larry”); We may alternatively do array assignment element by element $stuges[0] = “curley”; $stuges[1] = “more”; $stuges[2] = “larry”;

Hashes A hash can be thought of as an unordered set of scalars, accessed by some string value that is associated with each scalar. Here are two ways to declare a simple hash %longdays = (“Sun”, “Sunday”, “Mon”, “Monday”, “Tue”, “Tuesday”, “Wed”, “Wednesday”, “Thu”, “Thursday”, “Fri”, “Friday”, “Sat”, “Saturday”); %longdays = ( “Sun” => “Sunday”, “Mon” => “Monday”, “Tue” => “Tuesday”, “Wed” => “Wednesday”, “Thu” => “Thursday”, “Fri” => “Friday”, “Sat” => “Saturday” );

Opening Files STDIN is your program’s normal input channel STDOUT is your program’s normal output channel STDERR is an additional output channel for your program The various opens open(MYPIPE, “filename”);#read from input file open(MYPIPE, “<filename”);#same as above open(MYPIPE, “>filename”);#create and write to file open(MYPIPE, “>>filename”);#append to an existing file open(MYPIPE, “| output-pipe-command”); #setup an output filter open(MYPIPE, “input-pipe-command |”); #setup an input filter We close using close(MYPIPE);

Reading from a File and Standard In Simple example print STDOUT “Enter a number: “; $number = ; print STDOUT “The number is $number\n”; This program does not handle removing the new line from your typed input chop and chomp; chop will remove and return the last character passed to it (regardless of what it is) chomp will only remove the end of record marker chop example chop($number = ); alternative chop example $number = ; chop($number);

Arithmetic Operators Some Example Arithmetic Operators $a + $b $a * $b $a % $b $a ** $b Perl provides support for the usual set of mathematicl functions such as sine, cosine etc The O’Reilly Programming Perl book contains a nice alphabetical lists of the functions

String Operators String concatenation is handled by the Perl. operator Example $a = 345; $b = 655; print $a + $b; #prints 1000 print $a. $b; #prints There is also a string repeat or times operator Example $a = 123; $b = 2; print $a * $b; #prints 245 print $a x $b;#prints Three ways to handle output print $a. ‘ is equal to ‘. $c. “\n”; Uses the dot operator print $a, ‘ is equal to ‘, $b, “\n”; Uses list construct print “$a is equal to $b\n”; Uses interpolation

Assignment Operators Simple assignments $a = $c; $a = $c + 3; Assignment shortcut $a = $a * 3; $a *=3; This of course generalizes across most of the binary operators

Autoincrement and Autodecrement Operators Like C Perl supports the standard autoincrement and autodecrement operators ++$a, $a++ --$a, $a--

Logical Operators Perl supports the standard set of logical operators $a && $b $a || $b !$a $a and $b $a or $b not $a

Comparison Operators Numeric Comparison Operators == !- < > <= 0 if equal, 1 if $a greater, -1 if $b greater String Comparison eq ne lt gt le cmp

File Test Operators -e $aexits -r $areadable -w $a writable -d $adirectory -f $afile -T $atext file

Nature of Truth in Perl Any string is true except for ““ and “0” Any number is true except for 0 Any reference is true Any undefined value is false. Examples 0string 0 hence false 1string 1 hence true and converted to string 0 hence false 0.00same as above “”null string hence false “0.00”the string “0.00” true since neither empty or exactly “0” “0.00” + 0number 0 due to + coercion hence false \$aa reference to a hence true even if $a is false

Conditionals Perl supports the usual constructs if($state eq “Virginia”){ print “Southerner\n”; } elsif ($state eq “New York”){ print “Yankee\n”; } else{ print “Martian\n”; } Unless construct unless ($name eq “Jeff”){ print “It’s not me. \n”; }

for example for ($sold = 0; $sold < 500; $sold += $purchase){ $available = $sold; print "$available tickets are available. How many would you like:"; $purchase = ; chomp($purchase); }

while example while ($tickets_sold < 500){ $available = $tickets_sold; print "$available tickets are available. How many would you like:"; $purchase = ; chomp($purchase); $tickets_sold += $purchase; }

Average Grades Example open(GRADES, "grades") or die "Can't open grades: $!\n"; while ($line = ) { ($student, $grade) = split(" ",$line); $grades{$student}.= $grade. " "; } foreach $student (sort keys %grades) { $scores = 0; $total = = split(" ", $grades{$student}); foreach $grade { $total += $grade; $scores++; } $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; }

Debugging in Perl The first thing to note is we can ascertain a lot about our Perl code by executing it with the -w switch We can launch the perl debugger by invoking the -d option The debugger supports breakpoints single step next listing code trace

Recommended References Learning Perl (2nd Edition), R. L. Schwartz, T. Christiansen, and L. Wall, ISBN , O’Reilly and Associates Not as in-depth as the Programming Perl Reference Programming Perl (2nd Edition), L. Wall, T. Christiansen, and R. L. Schwartz, ISBN Definitive Reference