7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Introduction to C Programming
The Web Warrior Guide to Web Design Technologies
CGI Programming.
CGI Programming Part 2. Input Tags Many different ways of getting data from the user. The tag is used most often. has a type attribute –Specifies the.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Lecture 2 Introduction to C Programming
Objectives Using functions to organize PHP code
Introduction to C Programming
Introduction to C Programming
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
An Introduction to Programming with C++ Fifth Edition
VBA Modules, Functions, Variables, and Constants
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Introduction to JavaScript for Python Programmers
 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.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
A First Program Using C#
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
© 2000 – All Rights Reserved - Page 1 Introduction to JavaScript Programming Part Two.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
12 1 Redirection CGI/Perl Programming By Diane Zak.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Chapter 6 Sub Procedures
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to Programming the WWW I CMSC Winter 2003.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
JavaScript, Fourth Edition
10 1 Hidden Fields and CGI/Perl Programming By Diane Zak.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
CGS 3066: Web Programming and Design Spring 2016 PHP.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 9: Value-Returning Functions
Microsoft Access Illustrated
Subroutines Web Programming.
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
The Selection Structure
Chapter 10: Void Functions
CIS 136 Building Mobile Apps
Presentation transcript:

7 1 User-Defined Functions CGI/Perl Programming By Diane Zak

7 2 Objectives In this chapter, you will: Create and call a user-defined function Exit a script using the Perl exit statement Pass information to and receive information from a user-defined function

7 3 Objectives In this chapter, you will: Access the contents of an environment variable Learn how to code the repetition structure using the Perl while and until statements

7 4 Introduction Several built-in Perl functions have been used in the previous chapters: –print, push, chomp –length Used to return the number of characters stored in a scalar variable Syntax: length (variable); Have also used a function from the CGI.pm module –param Can also create your own functions –User-defined functions

7 5 User-defined Functions Reasons to create user-defined functions: –1. Allow a programmer to avoid writing and testing the same code more than once –2. Allow large and complex scripts to be broken into small and manageable tasks A team of programmers can work on a script, each programmer assigned a task which can be coded as a function

7 6 Functions Function (or subroutine) –Block of code that begins with keyword sub –Code for a function can be in the same script or can be in a separate file Examples: –Code for param is stored in CGI.pm file –Code for push is stored in perl/perl.exe

7 7 Functions Code for a function is processed when the function is called or invoked –Invoke a function by including its name and optional argument(s) in a statement Example: –$game = param (‘Game’); –param function is called and the argument is the Game key

7 8 Functions Each function has a specific task –After the function has completed, a value is returned to the statement that called it –The statement does not have to use the return value of the function function : –Syntax:

7 9 Functions function : –function header: Begins with sub keyword, space, function name, space, opening brace ({) –Same rules for naming functions as naming variables –Variable names should be easy to understand –function footer: Closing brace (}) –return statement: Optional –If there is no return statement, the function returns the value of the last statement it processes Can return multiple value(s) The function is terminated after the value(s) are returned

7 10 The Dombrowski Company Script –2 calls to user- defined functions: display_error_pag e(); display_acknowle dgement(); If there are no arguments to pass to a function, following the function name is an empty set of parentheses.

7 11 The exit; statement exit; terminates the script –Stops the script from processing further instructions –Not required, but a good programming practice The script will terminate after the statements have completed

7 12 The Temp-Employment Script –Depending on the value of $hourly_pay, a dynamic web page will be created The display_msg function is passed a string which will be written to the is a special array that contains arguments passed to that function –For example, if one argument is passed, can access it with $_[0]

7 13 O’Rourke Sales Script – Version 1 –If a variable is declared within a function, it is only available to that function –Many programmers consider it poor programming to have a function assign values to variables declared in the main part of the script More difficult to debug and troubleshoot

7 14 The O’Rourke Sales Script – Version 2 Variables are assigned values in main section of script

7 15 International Coffees Form and Dynamic Web Pages

7 16 –TEXTAREA tag is used to create a large text box, typically for comments International Coffees Form and Dynamic Web Pages

7 17 Planning the International Coffees Script

7 18 Environment Variables Series of hidden keys and values that the Web server sends to a CGI script when the script is run Can find out more information about the web server, user, and the user’s browser through environment variables Stored in the %ENV hash

7 19 Environment Variables

7 20 Coding the International Coffees Script –The REQUEST_METHOD key of the %ENV hash is checked to see if it is equal to “POST” The user clicked on the Submit Form button on the International Coffees form to get to the script.

7 21 Coding the International Coffees Script

7 22 while and until statements while statement: –Used to repeat a block of instructions while a condition is true –Syntax: while (condition) { statement(s) to process while condition is true } –Example: = (5000, 200, 100, 3); $x = 0; while ($x < 4) { print “$nums[$x]\n”; $x = $x + 1; } Result:

7 23 until statement: –Used to repeat a block of instructions until a condition becomes true –Syntax: until (condition) { statement(s) to process until condition is true } –Example: = (5000, 200, 100, 3); $x = 0; until ($x == 4) { print “$nums[$x]\n”; $x = $x + 1; } while and until statements Result:

7 24 Using while in the International Coffees Script –If a file is large, it can be a problem to use the angle operator (<>) to read a file into an array Each time the angle operator reads a record, it temporarily stores it in the $_ special variable. –Instead, can use while statement to read each record one at a time

7 25 Summary User-defined functions allow a programmer to avoid writing and testing the same code more than once. –Large and complex scripts can be broken down into smaller, more manageable tasks. A function is a block of code beginning with the Perl keyword sub. A function’s code is processed only when the function is called/invoked. –A function is invoked by including the function’s name and arguments in a statement.

7 26 Summary Every function performs a specific task and returns a value(s) to the calling statement. –The calling statement can use or ignore the return value(s). The length function can be used to determine the number of characters in a scalar variable. –Syntax: length (variable); A function definition is composed of: function header, function body, function footer. The exit statement can be used to terminate a script.

7 27 Summary The return statement is used in a function body to return a value(s) to the calling statement. –Syntax: return expression; –If there is no return statement, the function returns the value of the last statement it processes. Perl stores arguments passed to user-defined functions in array. It is considered poor programming to have a user-defined function assign a value to a variable declared in the main section.

7 28 Summary Environment variables are hidden keys and values that the Web server sends to a CGI script when the script is run –Stored in the %ENV hash The while statement is used to repeat one or more instructions while a condition is true. The until statement it used to repeat one or more instructions until a condition becomes true. The angle operator temporarily stores each record it reads in the $_ variable.