10.2.2 tie in General

10.2.2  tie in General

 The tie() function binds a variable to a class that provides the implementation of various methods for that variable. These are methods that construct objects of the class and provide access methods for the tied variable. Once the tie function performs the association, accessing a tied variable automatically triggers method calls in the proper class. The complexity of the implementation is all hidden from the user.

 In tie(), the first argument is the variable that is to be associated with the class. The second argument is the name of the class that implements objects of the correct type. The additional arguments are considered to be in a list and are passed into the constructor method for the class. For example, there is a method called TIEHASH() in any module that implements the tie link such as NDBM_File.pm. However, it is not necessary for us to know the details of how such a function works.

The variables that can be tied are

    scalars,

    arrays,

    hashes, and

    file handles.

We do not discuss much details of a class that implements a tie; interested readers are instructed to consult the perltie manual page in Perl documentation. In most computers, this should be accessible by typing

 

%perldoc perltie

 

The documentation is also available at the site www.cpan.org, The Comprehensive Perl Archive Network. 

A module such as NDBM_File.pm that implements a tie to a hash is required to implement at least the following methods in its definition.

 

  TIEHASH is the constructor method that returns reference to a new object. This method is automatically called when we make a call to tie in Perl. 

    FETCH is the access method. This method is automatically called when an element of the tied hash is accessed, i.e., read. 

    STORE is the method for writing. This method is automatically called when an element in the tied hash is set. 

  DELETE is the method that is triggered when we remove an element from the tied hash, e.g., by using the delete() function. 

    CLEAR is the method that is triggered when the whole hash is cleared. This is usually done by assigning the empty list. 

  EXISTS is the method triggered when in a Perl program, we use the exists() function on a tied hash.  

  FIRSTKEY and NEXTKEY are two methods that are triggered when a Perl program iterates through a tied hash using keys() or each(). 

    UNTIE is the method that is called when the Perl program calls untie(). 

  DESTROY is the method that is called when a tied hash is about to go out of scope.

 Any class or package that implements tie() to a hash must implement these methods. However, there is a module called Tie::Hash that provides an implementation of each of these methods. Therefore, anyone writing a class that ties a hash to a class needs to write only those methods that need to modified from those available in Tie::Hash.

    The DB_File, NDBM_File, GDBM_File, ODBM_File and SDBM_File modules provide implementation of the tie of a hash to a class. Therefore, each one of these classes needs to provide an implementation of the methods named earlier. An interested reader can examine the source code of these modules to see the definitions of these methods.