CMPS 335 Advanced Web Publishing Perl and CGI Programming


Perl Data and Variables

Quoting with qq

Perl 5 uses quotation mark characters and quote operators to identify character string data as described below:
   Identifier       Meaning
   ----------------------------------------------------------------
    " or qq   Begins a string.  Allows variable interpolation
    ' or q    Begins a string.  Does not allow variable interpolation
         qw   Quoted string.  Does not allow variable interpolation 
Using the qq operator, you don't need quotes to enclose a string.  This is useful for cases where you would otherwise have to escape the quotes using the (\") syntax as shown in the following examples:
    print "Mary says, \"Hello world!\"";
    print qq(Mary says, "Hello world!");  
    print qq|Mary says, "Hello world!"|;
    (qq will accept any character as a delimiter for the string)  

Perl Data Types

Perl data can be constants or variables.  Constants can be numbers or alphanumeric strings.  Variables are memory locations that can store either numeric or string values.  You do not need to declare a variable before using it.  Perl variables come in three different types:

    1. Scalar
    2. Array
    3. Hash


Scalar Variables

A scalar variable stores a single value, either numeric or string.  Scalar variables begin with a dollar sign ($).
Examples:
   [jhu@cs cgi-bin]$ cat scalarEexamples.cgi
   $count = 25;
   $count = $count + 1;
   print "$count\n";
   $count +=1;
   print "$count\n";
   $count++;
   print "$count\n";
   $name = "Janice";
   print "Hello, $name.\n";
   $rate = 12;
   $wage = $rate * 40;              # numeric multiply
   $string1 = "-" x 40;             # string multiply
   print "$wage\n";  
   print "$string1\n";
   $string2 = "South" . "eastern";  # string concatenation
   $string3 = "South" + "eastern";  # numeric addition
   print "$string2\n";
   print "$string3\n"; 
   [jhu@cs cgi-bin]$ perl scalarExamples.cgi
   26
   27
   28
   Hello, Janice.
   480
   ----------------------------------------
   Southeastern
   0             

Array Variables

An array is a collection of scalar values.  Array variables begin with an at sign (@). Unlike in most other languages, a Perl array can contain both numeric and string data.   Array indices start with 0.  When referring to a single array element, you prefix the array name with a $ instead of @ and surround the index with brackets.

There are three ways to obtain the length (size) of an array as shown in the following examples:
You can use the split function to split a scalar string into an array.  The syntax of the split function is:

   @arrayVariable = split(/pattern/,string);

where pattern is delimiter and string can be a scalar variable or a string constant.  The items in the string can be delimited by "&", ",", "=", etc.

Examples:
  
   [jhu@cs cgi-bin]$ cat arrayExamples.cgi
   #!/usr/bin/perl
   @colors = ("red","green","blue","black");
   @employees = qw(Mary, David, Janice);
   print "Employee No.1: $employees[0]\n";
   print "All employees: @employees\n";
   $colors[4] = "yellow";
   print "The last color in the array: $colors[4]\n";
   print "The last index of the array is $#colors\n";
   print "All colors in the array: @colors\n";

   $string1 = "number1=48&operator=x&number2=7";
   @pairs = split(/&/, $string1);  # Separate the name-value pairs at &
   print "The string array: @pairs\n";  
   $string2 = "number1=48\n";      # Separate the name-value at =
   ($name, $value) = split(/=/, $string2);
   print "$name -- $value\n";
   $len1 = $#colors+1;
   print "Number of colors: $len1\n";
   $len2 = scalar(@colors);
   print "Number of colors: $len2\n";
   $len3 = @colors;
   print "Number of colors: $len3\n";
   $length1 = $#employees + 1;
   $length2 = scalar(@employees);
   $length3 = @employees;
   print "Number of employees: $length1 - $length2 - $length3 \n\n";
  
   [jhu@cs cgi-bin]$ perl arrayExamples.cgi
   hash1 keys: a c -- values: b d
   hash2 keys: a c -- values: b d
   hash3 keys: e a c -- values: f b d

   Employee No.1: Mary,
   All employees: Mary, David, Janice
   The last color in the array: yellow
   The last index of the array is 4
   All colors in the array: red green blue black yellow
   The string array: number1=48 operator=x number2=7
   number1 -- 48   
   Number of colors: 5
   Number of colors: 5
   Number of colors: 5
   Number of employees: 3 - 3 - 3

Hash Variables

A hash is an associative array containing paired elements.  The first element in each pair is called the key and it is a label for the second element called value.  Hash names are prefixed with a percent sign (%).  To access each scalar element of the hash, prefix the hash name with a $ and surround the key with curly braces.

A hash can be defined as shown in the following examples: You can also define an individual hash pair as neededas shown below:    $hash3{'e'}="f"

Getting a value by using a key
Getting several values using keys
Getting all of a hash's values by using the values function Getting all of a hash's keys by using the keys function
Examples:
   [jhu@cs cgi-bin]$ cat hashExamples.cgi
   #!/usr/bin/perl
   %hash1 = ("a","b","c","d");
   %hash2 = ("a"=>"b","c"=>"d");
   %hash3 = ('a'=>"b",'c'=>"d");
   $hash3{'e'} = "f";
   @hash1keys = keys(%hash1);
   @hash1values = values(%hash1);
   @hash2keys = keys(%hash2);
   @hash2values = values(%hash2);
   @hash3keys = keys(%hash3);
   @hash3values = values(%hash3); 
   print "hash1 keys: @hash1keys -- values: @hash1values \n";
   print "hash2 keys: @hash2keys -- values: @hash2values \n";
   print "hash3 keys: @hash3keys -- values: @hash3values \n";
   @hash3valuessorted = sort(@hash3values);
   print "hash3 values, sorted -- @hash3valuessorted \n\n";
   %personal = ("age"=>26,"name"=>"Janice","address"=>"427 Main");
   %formdata = ("number1"=>48,"operator"=>"mul","number2"=>7);
   print "$personal{'address'}\n";   # Getting a signle value
   print "$personal{'name'}\n";
   print "$formdata{'number1'}\n";
   print "$formdata{'operator'}\n";
   $key1 = "address";
   $key2 = "number1";
   print "$personal{$key1}\n";       # Getting a signle value 
   print "$formdata{$key2}\n";
   print "@personal{'name','address'}\n";   # Getting multiple values
   @array1 = values(%formdata);  # Getting all values using values function
   @array2 = keys(%formdata);    # Getting all keys using keys function
   print "All values are: @array1 \n";
   print "All keys are: @array2 \n";
   
   [jhu@cs cgi-bin]$ perl hashExamples.cgi
@hash3valuessorted = sort(@hash3values);
print "hash3 values, sorted -- @hash3valuessorted \n\n";

%personal = ("age"=>26,"name"=>"Janice","address"=>"427 Main");
%formdata = ("number1"=>48,"operator"=>"mul","number2"=>7);
                               [ Wrote 33 lines ]

[jhu@cs cgi-bin]$ pico hashExamples.cgierl
   hash1 keys: a c -- values: b d
   hash2 keys: a c -- values: b d
   hash3 keys: e a c -- values: f b d
   hash3 values, sorted -- b d f
 
   427 Main
   Janice
   48
   mul
   427 Main
   48
   Janice 427 Main
   All values are: 48 7 mul
   All keys are: number1 number2 operator 

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