Previous Page Next Page

13.3. What's Next?

Now that you understand pointers, you will be ready learn how to create objects in Perl. The next chapter focuses on object-oriented Perl. It is a big chapter and extremely important if you are going to use modules from other libraries or CPAN, etc. You will learn how to create objects, assign properties to objects, use methods to manipulate objects, and how to destroy and reuse them. You will learn about @ISA and inheritance, closure, and garbage collection, etc. You will also learn how to document your modules with POD, Plain Old Documentation.

Exercise 13: It's Not Polite to Point!

1.Rewrite tripper (from Chapter 11) to take two pointers as arguments and copy the arguments from the @_ in the subroutine into two my pointer variables.
2.Create a hash named employees with the following three keys:

Name

Ssn

Salary

The values will be assigned as undefined (undef is a built-in Perl function). For example: Name => undef,

  1. Create a reference to the hash.

  2. Assign values to each of the keys using the reference.

  3. Print the keys and values of the hash using the built-in each function and the reference.

  4. Print the value of the reference; in other words, what the reference variable contains, not what it points to.

3.Rewrite the above exercise so the hash is anonymous, and assign the anonymous hash to a reference (pointer). Delete one of the keys from the hash using the reference (use the delete function).
4.Write a program that will contain the following structure:
$student = {  Name    => undef,
              SSN     => undef,
              Friends => [],
              Grades  => { Science => [],
                           Math    => [],
                           English => [],
                           }
           };

Use the pointer to assign and display output resembling the following:

Name is John Smith.
Social Security Number is 510-23-1232.
Friends are Tom, Bert, Nick.
Grades are:
          Science--100, 83, 77
          Math--90, 89, 85
          English--76, 77, 65


 

Previous Page Next Page