7 User-Defined Functions CGI/Perl Programming By Diane Zak 1 7 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 2 7 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 3 7 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 4 7 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 5 7 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 6 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 7 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: 8 7 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) are9 returned The Dombrowski Company Script 7 – 2 calls to userdefined 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. 10 7 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 11 The Temp-Employment Script 7 – 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] 12 O’Rourke Sales Script – Version 1 7 – 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 13 The O’Rourke Sales Script – Version 2 7 • Variables are assigned values in main section of script 14 7 International Coffees Form and Dynamic Web Pages 15 7 International Coffees Form and Dynamic Web Pages – TEXTAREA tag is used to create a large text box, typically for comments 16 7 Planning the International Coffees Script 17 7 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 18 7 Environment Variables 19 Coding the International Coffees Script 7 – 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. 20 7 Coding the International Coffees Script 21 7 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 } Result: – Example: my ($x, @nums); @nums = (5000, 200, 100, 3); $x = 0; while ($x < 4) { print “$nums[$x]\n”; $x = $x + 1; } 5000 200 100 3 22 7 while and until statements • 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; Result: 5000 200 100 3 23 7 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 24 record one at a time 7 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. 25 Summary 7 • 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 26 a script. 7 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 27 variable declared in the main section. Summary 7 • 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 28 record it reads in the $_ variable.