CSC3530 Software Technology Tutorial Two PERL Basics.

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

Introduction to C Programming
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
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.
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.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
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.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Perl Lecture #1 Scripting Languages Fall Perl Practical Extraction and Report Language -created by Larry Wall -- mid – 1980’s –needed a quick language.
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:
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
 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.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
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.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Introduction to Perl Yupu Liang cbio at MSKCC
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.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
PERL Ronald L. Ramos Proglan. What is PERL? Perl stands for Practical Extraction and Reporting Language. (or Pathologically Eclectic Rubbish Lister).
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
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introduction to Programming the WWW I CMSC Winter 2003.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
©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.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
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.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
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.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
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.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Information and Computer Sciences University of Hawaii, Manoa
Basic concepts of C++ Presented by Prof. Satyajit De
Control Structures: if Conditional
Control Structures: for & while Loops
Web DB Programming: PHP
Logical Operations In Matlab.
CSC 352– Unix Programming, Fall, 2011
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
INTRODUCTION to PERL PART 1.
Presentation transcript:

CSC3530 Software Technology Tutorial Two PERL Basics

Practical Extraction and Report Language It is interpreted (no need to compile) Java is something in between, both compile and interpreted Syntax similar to C language Loosely typed language No type like int, double, char, BOOL Why Perl for CGI? For the same CGI program, Perl is much shorter than C

Simple Example #!usr/bin/perl –w # this is a simple hello program print ‘Hi, I am computer. What’s your name?’; $name= ; chomp $name; print “Hi $name, nice to meet you\n”; exit(0); #!usr/bin/perl –w Location of perl interpreter, use whereis perl to get it -w switch to enable warning Place your comment after # All statements end with ;

Cont’d print – function to print strings to standard output (monitor) $name – all scalar variables in perl start with $ - read input from standard input (keyboard) - file handle to read input from file, will be described later chomp – function to remove new line character (\n) exit – end the program

Properties of Perl Function call may or may not need () No main function Do not need to declare variable at first Strings are enclosed by “” or ‘’ “” will try to evaluate variable inside e.g. “Hi, $name” while ‘Hi, $name’ will not This is call variable interpolation +,-,*,/,% similar to C ** means to the power of, e.g. $eight=2**3; # $eight will be 8

Boolean in Perl Things in Perl that considered as false Number zero$false=0; Empty string$false=“”; String zero$false=“0”; Undefined $false; Comparison ==, >, =, <=, !=, eq, gt, lt, ge, le, ne Logical operator &&, ||, !, and, or, not Short circuit open (FH,”file.txt”) || die “cannot open file” If file open is successful, the later statement will not be executed, otherwise, it will be executed and program terminated

Array in Perl Array in Perl start for ($i=0;$i<=$#terms;$i++) { print “$terms[$i] ”; } Output: > RSA DES OLAP OSI Middleware $terms[$i] – the (i+1) th element, array in perl counts from 0 $#terms returns the index of the last element in the array Can be replace DES OLAP OSI Middleware); qw is a function : quote word

Cont’d Another way to do DES OLAP OSI Middleware); foreach { print “$item “; }# more straight forward foreach { print “$_ “; } Both are the same, $_ is a special variable, many functions use it as default parameter,we will talk about this later, perhaps

Flow of Control Most are similar to C if (condition) {statement} elsif (condition) {statement} else {statement} while (continuing condition) {statement} for (A;B;C) {statement} A – initialization B – continuing condition C – statement to be executed every iteration Breaking the loop last – similar to break in C next – similar to continue in C

Cont’d Two examples while ($i<15) { last if ($i==7); $i++; } # $i in here equal 7; for ($i=0; $i<100; $i++) { next if (not $i % 2); print “An odd number =$i\n”; #only odd number goes here }

FILE I/O Simple file copy program #!/usr/bin/perl –w open(SOURCE,‘c:\\sourcefile.txt’) || die ‘Cannot open source’; open(DEST,’>c:\\destfile.txt’) || die ‘Cannot open ;#read all the lines into array print all the lines close(SOURCE); close(DEST); #can be shorter, replace 2 lines by: print DEST ; > sign is to indicate we want to write something to this is a little bit lazy, you may use a for loop instead print is to print all lines to the DEST file handle, if we omit it, the default is STDIN (monitor)

Hash structure Hash is like a table lookup e.g. telephone directory Hash structure begins with % %name=(‘3530’, ‘software technology’, ‘5280’, ‘image processing’, ‘5110’, ‘advance software eng.’); print “3530: $name{‘3530’}\n”; print “5280: $name{‘5280’}\n”; Output: 3530: software technology 5280: image processing Course codes are key, while course names are values Key value pair can be added dynamically $name(‘5180’)=‘data mining’; #a key value pair is added Dickson Richard Nelson {‘3530’}

More about hash print “We have these keys in our hash table\n”; foreach $key (keys %name) { print “$key\n”; } print “We have these values in our hash table\n”; foreach $value (values %name) [ print “$value\n”; } If (exists $name{‘5120’}) { print “we have key 5120 in the hash table\n”; } else { `print “we do not have key 5120 in the hash table\n”; } #correct ways to check whether the key,value pair exist

Split, Join Given a query string, How to retrieve the value input by user? ID=LJ4000&Category=printer&Price=700 $string=$ENV{‘QUERY_STRING’};#get the query for { ($name,$value)=split(/=/,$_); print “$name = $value\n”; } Output: ID = LJ4000 Category = printer Price = 700 Join will be discussed later in this tutorial

Functions (sub-routine) Perl allow recursion Functions in perl are defined like: sub function_name { statements; } Argument stored (a special array variable) You do not need to specify the number of argument Handle this carefully, you should know what you are doing Argument are all passed by reference Better use my to declare local variable

More about function sub mean { my my – indicate a local variable my $sum; foreach { $sum+=$_; } return } sub compare { my ($a, a way to retrieve parameters if ($a>$b) { return (1); } elseif ($a<$b) { return (-1); } else { return (0); }

Basic CGI We will introduce a perl CGI example that enable you to get values from a form and manipulate. www2.cse.cuhk.edu.hk/~csc3530/student_data.htmwww2.cse.cuhk.edu.hk/~csc3530/student_data.htm, use proxy.cse.cuhk.edu.hk and port 8000 use view source to check HTML code Name: id Name: name Name: major Name: level Name: csc1500,csc2510,csc2520

Simple example #!/usr/local/bin/perl5 -w use CGI qw(:standard);# use the CGI module use strict;# variable declaration more strict sub no_input { print header();# Content-type:….. print start_html('Error'); print qq( No input parameter ); print end_html(); } if (!param) {# check input parameter no_input; } else { my $id=param('id'); my $name=param('name');

Cont’d my $major=param('major'); my $level=param('level'); my $csc1500=param('csc1500'); my $csc2510=param('csc2510'); my $csc2520=param('csc2520');# get values open (STAT,">>/uac/cprj/csc3530/www/cgi-bin/stat.txt"); my $data=join (',',($id, $name, $major, $level, $csc1500, $csc2510, $csc2520)); print STAT "$data\n"; close (STAT); print header(); print start_html('Statistic'); print qq( Thanks for your support! ); print end_html(); }

Next Week Issues on making CGI to work Servers in CSE In depth discussion of CGI programming Example similar to project Supplier database SQL – (may be) insert, update, select