Download presentation
Presentation is loading. Please wait.
Published byGriffin Nelson Modified over 8 years ago
1
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak
2
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
3
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
4
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
5
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
6
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 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
8
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:
9
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
10
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.
11
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
12
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 page @_ is a special array that contains arguments passed to that function –For example, if one argument is passed, can access it with $_[0]
13
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
14
7 14 The O’Rourke Sales Script – Version 2 Variables are assigned values in main section of script
15
7 15 International Coffees Form and Dynamic Web Pages
16
7 16 –TEXTAREA tag is used to create a large text box, typically for comments International Coffees Form and Dynamic Web Pages
17
7 17 Planning the International Coffees Script
18
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
19
7 19 Environment Variables
20
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.
21
7 21 Coding the International Coffees Script
22
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: my ($x, @nums); @nums = (5000, 200, 100, 3); $x = 0; while ($x < 4) { print “$nums[$x]\n”; $x = $x + 1; } Result: 5000 200 100 3
23
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: my ($x, @nums); @nums = (5000, 200, 100, 3); $x = 0; until ($x == 4) { print “$nums[$x]\n”; $x = $x + 1; } while and until statements Result: 5000 200 100 3
24
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
25
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.
26
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.
27
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 the @_ array. It is considered poor programming to have a user-defined function assign a value to a variable declared in the main section.
28
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.