lecture in color

Perl Abstractions

Three abstractions

Pseudo-hashes

Ties

Tying a scalar

  1. define special methods TIESCALAR, FETCH, and STORE.
  2. TIESCALAR: called when scalar is tied.
  3. FETCH: called when user reads the value.
  4. STORE: called when user writes the value.
  5. place them in a class.
  6. tie class to a specific variable.

Example of scalar tying:

Advanced example: persistent file-storage of string

What constitutes a scalar:

  1. TIESCALAR: ability to initialize
  2. FETCH: ability to read a value
  3. STORE: ability to update a value
  4. DESTROY: ability to clean up after use

What constitutes an array:

  1. TIEARRAY: create one.
  2. FETCH: fetch a value.
  3. STORE: store a value.
  4. DESTROY: clean up afterward.
  5. FETCHSIZE: return number of elements.
  6. STORESIZE: set number of elements.
  7. EXTEND: increase size to given number of elements.
  8. EXISTS: whether an array element exists or not.
  9. DELETE: delete an element (make it not exist).
  10. CLEAR: make the array contain no data.
  11. PUSH: push data into array at top.
  12. POP: pop data off top.
  13. SHIFT: remove data from bottom of array.
  14. UNSHIFT: add data at bottom of array.
  15. SPLICE: array splicing.

Array tying:

  1. decide how you want behavior to differ from that of a regular array.
  2. implement any methods in which you want differing behavior.
  3. inherit all other methods from Tie::StdArray (the default array behavior).

Example: an array of numbers with default value

What constitutes a hash?

  1. TIEHASH: create one.
  2. FETCH: fetch a value.
  3. STORE: store a value.
  4. DELETE: delete a value.
  5. CLEAR: make the array contain no data.
  6. EXISTS: whether an array element exists or not.
  7. FIRSTKEY: return the first key of a hash.
  8. NEXTKEY: return the next key of a hash.
  9. DESTROY: clean up afterward.

Strategy for tying a hash

  1. decide upon deviant behavior
  2. implement required methods.
  3. inherit rest from Tie::StdHash.

What you've always wanted (for a2)

Extreme power: a hash that remembers history

What constitutes a filehandle?

  1. TIEHANDLE: create one.
  2. TELL: describe filehandle position.
  3. SEEK: seek to a position.
  4. PRINTF: call printf to a handle.
  5. READ: do a read of data from the handle.
  6. WRITE: do a write of data to the handle.
  7. EOF: whether the file is at EOF.
  8. BINMODE: whether to utilize raw mode or not.
  9. FILENO: returns the file descriptor number of the handle.
  10. DESTROY: how to destroy one when no longer needed.

Ties to databases

A simple database tie

Watch out!

The point

Objects

Predefined classes:

Methods with implicit invocation

Autoloading accessors

Another example:

As a finely tuned example,

Overloading

Example of overloading:

Overloading capabilities

Auto-generation

Special overloads

Copy constructor (=)

Example of what goes wrong:

How to fix this problem:


lecture in color


downloaded on Nov-23-2009 05:46:21 AM,
was last modified on Dec-31-1969 07:00:00 PM.

All lecture note content is copyright 2003 by
Alva L. Couch, Computer Science, Tufts University
(couch at cs dot tufts dot edu)