KW Perl Mongers Perl Modules: A Look Under the Hood #9

Using an OO Module

      use Cards;
      my $card = Cards->new( suit => 'H',
                             name => 'Q' );
      $card->set_face("back");
      $card->set_suit("D");
      $card->print;
      foreach $i (2..9) {
         push @deck, Cards->new ( suit => 'D', name => $i);
      }
      foreach (@deck) { $_->print; }
  • $card is a reference to something

    • Inside the module it can be a ref-to-array, a ref-to-hash, a ref-to-scalar, etc.

    • The user of the module doesn't know, and shouldn't care. (encapsulation)

      • unless they're extending the module themselves

  • Each element of @deck is a different instance.

  • Cards can be based on another OO module (inheritance)

    • inherit methods and/or attributes (data) which are accessed via methods.

<< Previous | Index | Next >> Copyright © 2004 Daniel Allen