COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 

Slides:



Advertisements
Similar presentations
A Guide to Unix Using Linux Fourth Edition
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bioinformatics is … - the use of computers and information technology to assist biological studies - a multi-dimensional and multi-lingual discipline Chapters.
COMP234 Perl Printing Special Quotes File Handling.
Chapter 4: Introduction to Perl and CGI programming. PERL -- Practical Extraction and Report Language Developed in 1980's by Larry Wall He created it to.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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 Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
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.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Introduction to Python
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
1 An Introduction to Perl Part 1 CSC8304 – Computing Environments for Bioinformatics - Lecture 7.
Writing Web Pages By Shyam Gurram. Agenda Writing Web Pages Delimiting PHP Program Units. Displaying Output to Web Pages Putting Comments in PHP Programs.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
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 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Strings, output, quotes and comments
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Chapter 6 JavaScript: Introduction to Scripting
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Variables, Expressions, and IO
Introduction to Scripting
Intro to PHP & Variables
Perl Variables: Array Web Programming.
Introduction to C++ Programming
Web DB Programming: PHP
elementary programming
Unit 3: Variables in Java
Introduction to Perl Learning Objectives:
Lexical Elements & Operators
Presentation transcript:

COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing  Running the program  Scalar Variables Operations and assignment Interpolation Exercise  Array variables Array assignments Displaying arrays Exercise

Perl Perl was created in the mid 1980’s by Larry Wall. The language was first dubbed ‘Perl’ and later it was decided that name stands for ‘Practical Extraction and Reporting Language’. (So it’s a ‘retronym’ not an acronym.) It’s been said that Perl fills a gap between high-level and low-level programming languages. Perl is especially useful for processing strings and other textual data. Regular expressions provide extremely flexible methods for processing text. Perl can be used for CGI programming (as we will see later). Example programs given in these notes can be found in the directory

A Basic Program Every Perl program starts off with #!/usr/local/bin/perl (or possibly #!/usr/bin/perl depending upon local server configuration). This is often referred to as the “shebang” line and directs the operating system to the location of the Perl interpreter. (There are no spaces in this line.) Single line comments can be added with the # symbol. (The first line above is not a comment, however.) A Perl statement must end with a semicolon ; The print function outputs some information. “\n” gives a ‘carriage return’. #!/usr/local/bin/perl # basic01.pl COMP519 # Program to do the obvious print “Hello world. \n”; # Print a message Here is a basic Perl program that we'll use to get started.

Running the Program Work under UNIX (Linux) or a suitable editor that can save files in ‘UNIX mode’. Use a text editor, e.g. vi or Xemacs with Perl mode (use `M-x perl- mode'). Make sure the file is executable by using the command chmod a+rx progname To run, type at the prompt perl progname or./progname (provided the “shebang” line is present) To run with warnings, use the command perl -w progname To run with a debugger, use the command perl -d progname Make sure your program works before proceeding. bash-2.05b$ perl basic01.pl Hello world. bash-2.05b$

Scalar Variables Scalar variables in Perl must start with $ and can hold both strings and numbers. These can interchange, so Perl is not a strongly typed language: $priority = 9; but you can later assign a string $priority = 'high'; $priority = “high”; Perl also accepts numbers as strings, like this (but strings are not the same as numbers): $priority = '9'; $default = '0009'; Variable names = numbers, letters and underscores, but they should not start with a number. Also, Perl is case sensitive, so $a and $A are different. $_ is a very special Perl variable, and we'll see more about it later.

Operations and Assignment For arithmetic, Perl includes: $a = 1 + 2; # Add 1 and 2 and store in $a $a = 3 - 4; # Subtract 4 from 3 and store in $a $a = 5 * 6; # Multiply 5 and 6 $a = 7 / 8; # Divide 7 by 8 to give $a = 9 ** 10; # Nine to the power of 10 $a = 5 % 2; # Remainder of 5 divided by 2 ++$a; # Increment $a and then return it $a++; # Return $a and then increment it --$a; # Decrement $a and then return it $a--; # Return $a and then decrement it For strings, Perl includes: $a = $b. $c; # Concatenate $b and $c $a = $b x $c; # $b repeated $c times # where $c is an integer To assign, Perl includes: $a = $b; # Assign $b to $a $a += $b; # Add $b to $a $a -= $b; # Subtract $b from $a $a.= $b; # Append string $b # onto $a etc. Note that when Perl (like most programming languages) assigns a value with $a = $b it makes a copy of $b and then assigns that to $a. Therefore the next time you change $b it will not alter $a. Other operators can be found on the perlop manual page. Type man perlop at the Unix (Linux) prompt.

Interpolation (substitution) of variables Note that print '$a and $b'; would literally print the string $a and $b, But we can use the double quotes like print "$a and $b"; to get the output apples and pears as in the programs above. The double quotes force interpolation of any codes, including interpreting variables and escape sequences (meaning that variables are replaced by their values). The newline code \n and the tab code \t are interpolated as well inside of double quoted strings. #!/usr/local/bin/perl # basic02.pl COMP519 $a = 'apples'; $b = 'pears'; print ‘$a and $b’; print “\n”; #!/usr/local/bin/perl # basic03.pl COMP519 $a = 'apples'; $b = 'pears'; print “$a and $b \n”; bash-2.05b$ perl basic02.pl $a and $b bash-2.05b$ bash-2.05b$ perl basic03.pl apples and pears bash-2.05b$

Exercise This exercise is to rewrite the Hello world program so that (a)the string is assigned to a variable and (b)this variable is then printed with a newline character. (c)Use the double quotes and don't use the concatenation operator. #!/usr/local/bin/perl # basic04.pl COMP519 $message = 'Hello world.'; print “$message \n”; # Print a message bash-2.05b$./basic04.pl Hello world. bash-2.05b$

Examples #!/usr/local/bin/perl # basic05.pl COMP519 print '007',' has been portrayed by at least ', 004, ' actors. '; print "\n"; print 7+3, ' ', 7*3, ' ', 7/3, ' ', 7%3, ' ', 7**3, ' '; print "\n"; $x = 7; print $x; print "\n"; print ' Doesn\'t resolve variables like $x and backslashes \n. '; print "\n"; print " Does resolve $x and backslash\n"; $y = "A line containing $x and ending with line feed.\n"; print $y; $y = "Con". "cat". "enation!\n"; print $y; bash-2.05b$./basic05.pl 007 has been portrayed by at least 4 actors Doesn't resolve variables like $x and backslashes \n. Does resolve 7 and backslash A line containing 7 and ending with line feed. Concatenation! bash-2.05b$

String Functions Here are some built-in Perl functions for operating with strings: Name Parameters Result chomp (a string) the string with terminating newline characters removed length (a string) the number of characters in the string lc (a string) the string with uppercase letters converted to lower uc (a string) the string with lowercase letters converted to upper hex(a string) the decimal value of the hexadecimal number in the string join(a char. and the strings concatenated together with the character a list of strings) inserted between them q /a string/ places single quotes around the string qq /a string/ places double quotes around the string

Array Variables an array is a list of scalars, i.e. numbers and strings. Any Perl array variable is prefixed by symbol. Notice that has changed to a $ because “eels” (a single element of the array) is a = ("apples", "pears", "eels"); # a three element = ("whistle", "flute"); # a two element list $food[2] # returns = ("whistle", = "harp"); # combines = ("organ", "whistle", "flute", "harp"); # is the = qq(organ whistle flute harp); # is the same Note: Perl only has single dimensional arrays (you can get more dimensions by using references, but I (probably) won’t discuss that in this course).

Push and Pop = ("apples", "pears", "eels"); "eggs"); # pushes eggs onto the end "eggs", "lard"); # pushes two items ("eggs", "lard")); # pushes two items (same as # pushes the items # onto the # onto # list and $length contains the # length of the new list push is a neater way of adding array elements (to the end) pop is a way of removing the last item from a = ("apples", "pears", "eels"); $grub = # Now $grub = "eels“ shift and unshift work in a similar manner but operate on the front of the list

Array Assignments ($a, $b) = ($c, $d); # Same as $a=$c; $b=$d; ($a, $b) # $a and $b are the first two items # $a is the first item and is a list of the remaining $a) and # $a is undefined, that form is best avoided ($a, $b) = ($b, $a); # swaps the values of $a and $b (since # Perl creates the whole list before the # assignment statement) $f= $#food; # assigns the index of the last element array $f # assigns the length to the variable $f $f = # turns the list into a string with a space between each element. # This space can be replaced by any other string # by changing the value of the special (built-in) $" variable. Can make multiple assignments to scalar variables Can make an assignment to a scalar variable in different contexts.

Displaying Arrays Context is important in determining the output! #!/usr/local/bin/perl # basic06.pl = ("apples", "pears", "eels"); # By itself (no spaces appear between array elements) print "\n"; print # Embedded in double quotes (spaces appear) print "\n"; # In a scalar context, gives the length of the array print "$f \n"; bash-2.05b$ perl basic06.pl applespearseels 3

Perl has many special (built-in) variables. The most commonly used one is the scalar variable that has the name $_ This is the default variable that is used when an argument is not provided to a function. The special $_ variable

For example, the function call print; is equivalent to the function call print $_; and the function call chomp; is equivalent to chomp $_; or chomp($_); This built-in variable is a shortcut provided to the programmer and is often heavily used in Perl programs (which can therefore be confusing to new programmers). The special $_ variable (cont.)