Invoking a Tie
Invoking a tie() is similar to other Perl functions. tie() uses this syntax:
tie VARIABLE, CLASSNAME, LIST
VARIABLE is the variable being tied, which could be a hash, array, scalar, or filehandle.
CLASSNAME is the class we're using to handle the tied object.
LIST consists of any optional flags or arguments that the class can use to construct the tied variable.
If you're always tying a variable to the same resource, and this resource is predefined in the class, you don't need to include the name of the resource in LIST. If the specific resource could change (different filename, different database, different machine, and so on), one of the elements of the LIST will be the name of the resource. After calling tie(), the class returns an object, which can be either stored or ignored. Either way, when you access the VARIABLE, methods are called on the object.
A common misconception after looking at the syntax for the tie function is that tie() itself uses the class that's handling the tie. This is incorrect, and the programmer must remember to use() the appropriate module.
Don't confuse the type of the variable and the type inside the object. You can bless() any type of Perl variable to make your object, and this has nothing to do with the type of variable being tied to. You can tie a hash to a blessed hash, or tie a hash to a blessed scalar, or tie a handle to a blessed scalar, or anything else that makes sense for your application.
To see what this looks like in practice, below is an example of tying a hash to the current directory using the Tie::Dir class. The result will be a tied interface between %hash and an object in the Tie::Dir class. The Tie::Dir class' methods will handle the manipulation of the directory via %hash:
use Tie::Dir; tie %hash, Tie::Dir, "./";