Item 52: Create invisible interfaces with tied variables .Let's end our discussion of object-oriented programming with an example. A tied variable in Perl is a variable that has been given magical properties via the tie operator. A tied variable is bound to a Perl object. All of the operations on that variable, such as assignment to it, reading its value(s), iterating over its values, and so on, are translated into method calls for that object. Perl supports tying the following types of variables:
Let's just dive right in and look at an example. I've tried to explain it reasonably thoroughly on the fly, but this isn't intended to be a complete description of how to write a tied class. For that you should consult the perltie man page. A tied hashWe will create a class called FileProp that allows you to access certain properties of a file by accessing the elements of a hash. To keep things simple, let's support just the following:
There are nine methods to write for a tied hash: TIEHASH , FETCH , STORE , DELETE , CLEAR , EXISTS , FIRSTKEY , NEXTKEY , and DESTROY . You don't have to write all of them if you will be using only some of the functionality of a hash (see Item 58 for an example of this), but you should go the full Monty if you are writing a tied hash class for general use. The first step is to create a constructorthe TIEHASH methodfor the FileProp class: The FileProp prologue and constructor
You tie variables to the FileProp class using the tie operator: tie %data, FileProp, "new.data"; Then tie makes a class method call to TIEHASH : FileProp::TIEHASH "FileProp", "new.data"; TIEHASH returns a Perl object (in this case, a blessed hash ref), which tie magically binds to the tied variable ( %data , above). Now, all accesses to the variable %data will invoke FileProp methods. Let's write some of those methods. The FETCH method is called when a value is retrieved from the tied variable: The FileProp FETCH method
With the FETCH method in place, you can say: print "size of data = $data{size}\n"; and the size of the file "new.data" will be displayed. Next up is the STORE method. The STORE method is called when a value is assigned to the tied variable: The FileProp STORE method
With the STORE method working, you can say: $data{contents} = "Testing one two three\n"; and the contents of the file will be overwritten with the string "Testing one two three\n" . Or, perhaps you would like to change the modification time to two minutes ago: $data{mtime} = time - 120; It gets simpler from here. Let's define the methods used to test and iterate over keys: The FileProp EXISTS , FIRSTKEY , and NEXTKEY methods
Now you can find out what properties are supported by FileProp : print "properties: ", join(" ", keys %data), "\n"; which will give you: name ctime size mtime contents Finally, although they don't make a lot of sense for our application, we have to define methods for deleting and clearing the tied hash. We will just have them produce errors: The FileProp DELETE and CLEAR methods
A destructor (the DESTROY method) is not necessary for this example. Let's demonstrate the completed class. First, some driver code (just add it to the end of the file): package main; tie %data, FileProp, "new.data"; $data{contents} = "Demo data"; foreach (sort keys %data) { print "$_: $data{$_}\n"; } When run, this should produce output similar to the following: % tryme contents: Demo data ctime: 873187477 mtime: 873187477 name: new.data size: 9 That's all there is to it! For another tied variable example, see Item 58. |