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.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
COMP234 Perl Printing Special Quotes File Handling.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
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 Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Python
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.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Introduction to scripting
 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.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
AIT 616 Fall 2002 PHP. AIT 616 Fall 2002 PHP  Special scripting language used to dynamically generate web documents  Open source – Free!!!  Performs.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
FTP (File Transfer Protocol) & Telnet
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 6.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
USING PERL FOR CGI PROGRAMMING
Building PERL Scripts on a Windows system* *and running those scripts on an Apache server!
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Input, Output, and Processing
Website Development with PHP and MySQL Saving Data.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CGI Common Gateway Interface. CGI is the scheme to interface other programs to the Web Server.
COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
CSU - DEO Introduction to CGI - Fort Collins, CO Copyright © XTR Systems, LLC Introduction to the Common Gateway Interface (CGI) Instructor: Joseph DiVerdi,
Variables, Expressions and Statements
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Intermediate CGI & CGI.pm Webmaster II - Fort Collins, CO Copyright © XTR Systems, LLC CGI Programming & The CGI.pm Perl Module Instructor: Joseph DiVerdi,
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
1 JavaScript in Context. Server-Side Programming.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CS 330 Class 8 Homework A pattern that contains a word with an optional period A pattern that contains Fred with a space (not Freddy) See regexp.txt guest4.htm.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
The Common Gateway Interface (CGI) Pat Morin COMP2405.
28 Formatted Output.
CS 330 Class 7 Comments on Exam Programming plan for today:
Introduction to Programming the WWW I
Intro to PHP & Variables
Topics Introduction to File Input and Output
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Introduction to Perl Learning Objectives:
Topics Introduction to File Input and Output
Presentation transcript:

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 help with text parsing tasks on UNIX Became very popular for server-side Web programming because of platform independence and text parsing ability.

Running Perl programs From command line: Unix/Linux, Active Perl on Windows Using an IDE: MacPerl As CGI program via HTTP request A simple "hello world" Perl program.

Run from command line If you are already in the directory containing the program > perl hello.cgi Or supply the full path to the program > perl /home/students/jones/perl.cgi

Strings -- can be double or single quoted Double quoted strings Escape sequences are interpreted print Variables are interpolated $num=10; print "The number is $num"; The number is 10

Single quoted strings Escape sequences are NOT interpreted (except two cases \' and \\ ) and variables are NOT interpolated print 'Scooby\nDoo\n'; Scooby\nDoo\n Ideal for printing HTML code which has lots of quotes Single print ' Scoob ',"\n"; Double print " Scoob \n";

Scalar variables Preceded with $ character $num = 10; $name= "Raul"; Can hold strings or numbers (no formal Boolean type literal) $x = "hello"; $x = 3; $x = 3.14; Perl is loosely typed (like JavaScript) $x = "3"; $y = "4"; $z = $x * $y; # on-the-fly conversion -- result of 12

Concatenation -- Uses the period (.) $x="base"; $y="ball"; $z=$x.$y; # "baseball" Smooth on-the-fly conversions $x=21; $y=12; $z=$x.$y; # "2112"

Some weird on-the-fly conversion examples: Perl tries to choose the proper context for an operation.

Function calls: functionname(argument) # standard notation In Perl, the parentheses are optional! functionname argument # crazy man Some built-in functions: $x = sqrt 36;# 6 $x = int 2.77# 2 (truncation) $str = "hello"; length $str; # 5

chop removes the last character and returns it $str = "Hello\n\n"; $x = chop $str; # $str contains "Hello\n" -- $x contains "\n" chomp removes ALL new line characters at the end of the string and returns the number of characters "chomped" $str = "Hello\n\n"; $x = chomp $str; # $str contains "Hello" -- $x contains 2 chomp is VERY useful when processing text files

The input operator <> Returns one line from standard input (the keyboard) including the \n $oneline = <>; We will later grab lines from text files by doing $oneline = ; See greeting.pl, quotient.pl, and nouns.pl

Formatting numeric output with printf printf "round to two decimals %.2f and no decimals %.0f", , ; round to two decimals 1.67 and no decimals 3 The % is the field specifier and f stands for float The specified fields are replaced (in order) with the numbers supplied at the end after the formatting has been applied

Integer formatting $x=4.99; $y=7; printf "integer: %d funky integer: %03d", $x,$y; integer: 4 funky integer: 007 Think of the d as standing for digit

Saving formatted numbers using sprintf (printf only prints them) Example1: $cost=" "; $formattedCost = sprintf "\$%.2f", $cost; # $formatted cost contains "$19.33" Example2: $hours=4.05; $minutes=3; $time = sprintf "%d:%02d", $hours, $minutes; # $time contains "4:03"

A "Hello World" CGI program

Can run as Non-CGI program (from command line or in an IDE)

All the new lines (\n) in the program help the HTML output to be readable. Otherwise you get something like: Remember: This output has to be read by yet another application -- a Web browser). The browser doesn't care about extra whitespace in the HTML, but you may need to debug the HTML when the resulting Web page doesn't look right or work correctly.

The "hello world" CGI program executed via an http transaction.

Notes: A Web browser tells(URL request) the server software to execute the program. The server software MUST return a viable message to the browser to complete the http transaction. If the server does not get back proper output from the CGI program, it has some pre-made error pages to send back to the browser.

How the CGI program can fail to return a proper message to the Web server software. 1. You forget the shebang line #! /usr/bin/perl Result: The server software can't find to Perl interpreter to run the program. The server returns an error page to the browser saying Internal Server Error # 500.

2. You forget to print the Content-type line or write it incorrectly. print "Content-type: text/html\n\n"; Result : Internal Server Error # 500 The server software needs the Content-type line to complete the http header -- something like: HTTP/ OK Date: Fri, 30 Nov :24:33 GMT Server: Apache/1.3.1 Content-length: 341 Content-type text/html … blank line containing only a newline character … data returned from the CGI program (i.e. the HTML page) The server software adds all but the Content-type like followed by a blank line. You MUST supply that.

3.The CGI program does not have permissions set so it can be executed by an anonymous user (anyone). Result : Forbidden: Server Error # 403 On Unix/Linux, proper permissions are OwnerGroupAnyone rwx rx rx Telnet to the server, navigate to the directory containing the file, and do a change mode on the program  chmod +rx filename.cgi or  chmod 755 filename.cgi Some ftp programs will let you set permissions

4. The program tries to do something illegal like: -- Using <> to try to read keyboard input -- Trying to open a non-existent file Result : Internal Server Error # 500 One more potential problem: You don't get one of the server software's pre-made Web pages, but the browser gets back a Web page which doesn't display properly (or at all). Cause: The program is generating bad HTML. The Web server software does not care about this. The server software does not read the HTML. It is up to the browser to try to display what it gets.

Print Block -- A handy way to print larger chunks of HTML. $value="1234 Maple Ave."; print <<AFORM; AFORM Has best features from printing single and double quoted strings Escape seqences are interpreted (like double quoted strings) Scalar variables are interpolated (like double quoted strings) Quotes (") don't need to be escaped (like single quoted strings) Use any legal variable name -- the convention is to use upper-case and to be descriptive. NEVER indent the closing label.