Perl/CGI Scripting Exercise 3

Subroutines

 

1.   A subroutine is a block of statements that will execute when you call it in your code.  This is easier than copying and pasting code on every single for common tasks that you will do (such as parsing forms).

2.   To create a simple subroutine simply type in the following

 

3.   To call a subroutine, simply type &my_sub_name; in my program.  For example, I will create a subroutine that simply prints out the MIME content so the Perl interpreter will know this is HTML code.  I called this file sub_test_1.cgi and placed it in the /var/www/cgi-bin/ directory:

 

 

4.      To create a subroutine that takes input, you will use the $_[] scalar to access the elements passed to the subroutine.  For example $_[0] will contain the first argument passed to the subroutine, $_[1] will contain the second argument, and so on.  If you want to access the whole input array, simply refer to it as @_.  Look at the following example.  Notice the call for the subroutine in sub_test_2.cgi.

 

 

5.   If you want to use a value returned from a subroutine, you will need to save the return value in either a scalar or an array.  Instead of calling the subroutine with a simple $my_sub_name, you call the subroutine by declaring a variable and then setting it equal to the subroutine call.  Look at the following to see how this is accomplished.

 

6. Now we can create a "library" that will contain common subroutines that we will use on a regular basis.  Open your file called parseform.cgi and copy and paste the code into a new file called parseform.lib (lib is the standard extension for a library in CGI/Perl scripting).  Remove the shebang line and add the following to the top:

 

 

At the bottom of the file add the closing bracket to end the subroutine followed by a return value of 1 (just in case).

 

 

If that didn’t work, copy and paste the .lib from here.

7.   To call a subroutine that is in the .lib file, you simply add the following to the top of your .cgi file:

 

8.   Now you can call the subroutines contained in this file just as if they were listed at the bottom of your current file.  For example, parseform.lib contained a subroutine called parseform.  To call the subroutine in any .cgi file, include the following:

 

 

  1. Note that you MUST put the 1; at the end of your .lib files in order for the subroutines to work correctly.  You will get a "Premature end of script headers" error if you forget to add the 1; to the end of a .lib file.

 

On Your Own Exercise 3

 

·        Create three subroutines called mime, header, and footer.  The mime subroutine should print out the Content-type:text/html\n\n line required by CGI scripts.  The header subroutine simply prints out the top of a normal .html page (<html><head>...<body>).  The footer subroutine will print out </body></html> to go at the bottom of the page.  Put all of these subroutines in a file called html_subs.lib.

·        Next, create a subroutine called information and place it in the actual .cgi file.  This subroutine should print out your name, email address and favorite color.

·        Call these subroutines in the following order:

mime

header

information

footer

 

·        You MUST comment your code to receive full credit.  Turn in both your html_subs.lib and .cgi file.