Team LiB   Previous Section   Next Section

Perl's Object Orientation

So far, we've covered the whole of basic Perl in fewer than 20 pages (Perl purists will be horrified at how much we've skipped over!) Now we're ready to engage hyper-drive and head toward Perl's object orientation zone.

Packages

In order to provide the Holy Grail of code reusability, Perl provides plenty of scope for the creation of software packages. These packages are generally held in a Perl module file, such as MyPackage.pm, which can be accessed from a main script like this:

use MyPackage;

A basic package skeleton can look as simple as this:

package MyPackage; 
use strict; 
  
sub doSomeStuff      {  # Some subroutine code here } 
sub doSomeOtherStuff {  # Some other subroutine code here }

To create our package, all we needed to do was name it and create some methods. And that was it.

Bless this Object

To go beyond packages and into object territory, we use the wizardry of the bless command. We'll extend our earlier skeleton and make it into a real class by adding a new constructor containing this most magical of commands:

package MyPackage; 
use strict; 
  
sub new {
   my($class) = @_;
   my $self = {};        # Anonymous hash! :-)
   bless $self, $class;
   return $self;
}
sub doSomeStuff      {  # Some subroutine code here } 
sub doSomeOtherStuff {  # Some other subroutine code here }

There's an awful lot going on within those few lines of code in the new method. We've broken it down into 11 life-cycle steps, listed below and illustrated in Figure A-6.

Figure A-6. Object-oriented life cycle in Perl
figs/pdba_aa06.gif
  1. To initiate the life cycle we call the use MyPackage command to import the package from the Perl library.

  2. The package imports into memory and stands ready for action.

  3. If it were a more complex package, MyPackage could multiply inherit from other objects in the Perl library.

  4. The new method is called in the main script, requesting that a key be returned to a brand new object generated by the packaged class constructor:

    $key = MyPackage->new(  );
  5. As we've seen, this new method contains the following code:

    my($class) = @_;
    my $self = {};
    bless $self, $class;
    return $self

    When the arrow notation is used with a package name to call a method, such as new, the package name gets sent automatically into the method as the first parameter of the special @_ subroutine parameter array. We take this string `MyPackage' and assign it to the $class scalar variable. We then create an anonymous hash with the {} notation. This effectively becomes the object for the rest of the life cycle, and is used later to store important object state information.

  6. We take a reference key to this anonymous hash object and store it in the lexically scoped $self variable:

    my $self = {};
  7. The magical bless command now associates the object with the class name:

    bless $self, $class;
  8. With the class name firmly labeled to the object, the referential key is returned to the calling program. The anonymous hash will continue to exist as long as at least one reference is pointing to it:

    return $self;
  9. The program can now use the returned key to drill down to the object's class and call its various methods to do useful work.

  10. The calling program eventually undefines the key or lets it go out of scope.

  11. With a reference count of zero, the original object has become a bubble of memory entirely disconnected from the outside world. Because it's of no more use to anyone, it is quickly gobbled up by Perl's garbage memory collector. The object's life cycle is complete.

After this lightning tour of basic Perl, we're now ready to start using Perl's object-oriented packages with confidence, including Perl DBI (whose features are summarized in Appendix B).

    Team LiB   Previous Section   Next Section