CMPS 335 Advanced Web Publishing Perl and CGI Programming


Functions

Individual statements are often gouped into logical units called procedures.  There are two major types of procedures used in Perl: functions and subroutines.  In general, system-defined procedures are called functions and user-defined procedures are subroutines.   A function may have operands called arguments or parameters.  Arguments are usually enclosed in parentheses and are separated by commas if there are more than one.  A function returns a value as shown in the following examples:
a. shift(@array); If @array contains (a,b,c), the result of shift(@array) is (b,c) and the return value is a.
print statememnt; The return value of any print statement is 1 if it is successful. ; Some important functions are listed below.

Array Functions
   Function   Syntax                      Description
  -------------------------------------------------------------
   pop        $var = pop(@arrayname);
       Returns the last value of the array
   push       push(@arrayname, element); 
       Adds the element to the end of the array
   sort       sort(@arrayname);
       Sorts the values of the array alphabetically
   reverse    reverse(@arrayname);
       Reverses the values of the array alphabetically  
   scalar     $var = scalar(@arrayname); 
       Returns the size of the array

   Examples 
   @colors = ("red","blue","green");
   print "@colors\n";
   $size = $#colors + 1;     # array size using $#arrayName+1
   print "There are $size colors\n";
   $size = scalar(@colors);  # using scalar function
   print "There are $size colors\n";
   @reversedColors = reverse(@colors);
   print "@reversedColors\n";
   $color = pop(@colors);
   print "$color\n";
   print "@colors\n";
   push(@colors,"black");
   print "@colors\n";
   @sortedColors = sort(@colors);
   print "@sortedColors\n";

   Output
   red blue green
   There are 3 colors
   There are 3 colors
   green blue red
   green
   red blue
   red blue black
   black blue red   

Hash Functions
   Function   Syntax                      Description
  -------------------------------------------------------------
   delete     delete $hashName{$key}
       Deletes the specified key/value pair and returns    
       the deleted value
   exists     exists $hashName{$key}
       Returns true if the specified key exists in the hash
   keys       keys %hashName or keys(%hashName)
       Returns a list of keys for that hash
   values     values %hashName or  values(%hashName)
       Returns a list of values for that hash   
   scalar     scalar %hashName
       Returns true if the hash has any element defined  

   Examples
   %personal = ("age"=>26,"name"=>"Janice","address"=>"427 Main");
   @values = values(%personal);  # Getting all values
   @keys = keys(%personal);      # Getting all keys
   print "Keys are: @keys \n";
   print "Values are: @values \n";

   Output
   Keys are: name age address
   Values are: Janice 26 427 Main    

String Functions
   Function   Syntax                      Description
  -------------------------------------------------------------
   chomp      variable = chomp($scalar);
       Removes a newline character from the end of a string
   chr        variable = chr(number);
       Translates a number into a character
   length     variable = length(expression);
       Returns the number of characters in a string
   m//        $searchedString =~ m/pattern/;
       Returns true value if the pattern is matched in the $string
   pack       pack(TEMPLATE, inputList);
       Takes a character code and translates a corresponding character
       to the format specified
   split      variable = split(/pattern/,expression);
       Separates the expression into parts
   s///       $searchedString =~ s/oldPattern/newPattern/;
       Replaces one string for another string
   sbstr      variable = substr(expression,startingPoint,length);
       Returns or modifies a substring
   tr///        $string =~ tr/searchString/replacementString/;
       Translates every character in search string to its corresponding 
       character in replacement string
   uc         variable = uc(expression);
       Changes all the characters in the expression to uppercase  


The time and localtime Functions

The time function returns a 4-byte integer denoting billions of seconds since January 1, 1970.  The localtime(time) function returns the local time parts in the specific order as shown below:    sec,min,hour,dayOfMonth,month,weekday,year,dayLightSaving
where
   Example:
  [jhu@cs ~/public_html/cgi-bin]$ cat perltime.cgi 
  #!/usr/bin/perl
  print "Content-type:text/html\n\n";
  ($sec,$min,$hour,$dayOfMonth,$month,$weekday,$year,$dayLightStandardTime)
     = localtime(time);
  $month = $month+1;
  $year = $year + 2000;
  $dayOfMonth = $dayOfMonth +1;
  print "$month/$dayOfMonth/$year\n";
  # End perltime.cgi 
 
  [jhu@cs cgi-bin]$ perl perltime.cgi
  10/16/2001 

The tr/// Function

The translate function is a general purpose character-translation function.  It exchanges each occurrence of a character in the search string with its corresponding character in the replacement string.  The basic syntax of the translate function is:
   Examples
   $string1 = "Hello.  My name is David.";
   $string1 =~ s/David/Kenneth/;
   print "$string1\n";

   $string2 = "Southeastern+Louisiana+University";
   $string2 =~ tr/a-z/A-Z/;
   print "$string2\n";

   $string2 =~ tr/+/ /;
   print "$string2\n";

   $s1 = "a,b,c.d.e";
   $s1 =~ tr/\,\./-/;
   print "$s1 \n";
   $s2 = "a,b,c.d.e";
   $s2 =~ tr/,./-/;
   print "$s2 \n";
   $s3 = "a,b,c.d.e";
   $s3 =~ tr/,./+-/;
   print "$s3 \n";
   $s4 = "a,b,c.d.e";
   $s4 =~ tr/,./ /;
   print "$s4 \n";
   $s5 = "a,b,c.d.e";
   $s5 =~ tr/,.//;
   print "$s5 \n";
   
   (Output)
   Hello.  My name is Kenneth.
   SOUTHEASTERN+LOUISIANA+UNIVERSITY
   SOUTHEASTERN LOUISIANA UNIVERSITY   
   a-b-c-d-e
   a-b-c-d-e
   a+b+c-d-e
   a b c d e
   a,b,c.d.e   

The functions m//, split, and s/// are three primary functions that use regular expressions.  They provide powerful string manipulation capabilities and are discussed in the chapter on Regular Expressions.

Return to CMPS 335 Home Page
Return to Web Site Home Page