GoCool has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to learn object oriented perl since a few days and i've been doing considerably well except for one thing (or so i think!).
I'm not able to get what exactly is the difference between Tie and Bless in perl.
As far as the function prototype goes i know bless takes in reference to a variable (i stress on it being a varaible, i.e. hash or list or scalar, correct me if i'm wrong) and optionally the name of the package and blesses it (whatever that means) into the package.
Tie on the other hand takes in two arguments, a variable and the name of the class that it ties the variable into and optionally any arguments that needs to passed to the constructor of that class.
I want to know in terms of the symbol table manipulation or package namespace etc what this blessing or tieing operation is actually doing?
Is it just making the reference to the object belong to the class by putting it into its namespace? is it just appending the name of the class and '::' to the variable?
what is going on beneath the hood in these two cases?
i appreciate all your help.

Replies are listed 'Best First'.
Re: Difference between tie and bless
by dave_the_m (Monsignor) on May 27, 2005 at 19:50 UTC
    Blessing is the usual technique for creating all objects. All it does is mark the 'thingy' (scalar, hash or whatever) pointed to by the reference as belonging to a particular package. Doing this allows the method call syntax to work, ie
    $ref->method(@args)
    is shorthand to tell perl to execute
    Foo::Bar::method($ref, @args)
    where Foo::Bar is the package that the thingy pointed to by $ref has been blessed into. If the 'method' sub doesn't exist in Foo::Bar, then Perl looks in the packages listed in @Foo::Bar::ISA for a sub called 'method', and so on.

    Tying is something completely unrelated, and only has a marginal conection with OO (in the sense that it's implemented using OO). It's used when you want to take control of how a variable is accessed.

    tie @arr, Foo::Bar, @args
    calls
    Foo::Bar->TIEARRAY(@args)
    and this sub is supposed to return a blessed object of some type (not necessarily an array). Perl then attaches this object to the variable in question (@arr), and marks it as being tied. Any subsequent access of this variable (eg $arr[3] = 4) is intercepted, and rather than modifying the contents of the real array, a method is called on its associated object, eg
    $hidden_object->STORE(3,4);

    Dave.

Re: Difference between tie and bless
by Zaxo (Archbishop) on May 27, 2005 at 19:41 UTC

    Bless just tags the variable with a namespace. The effect is that perl looks in that namespace (and its @ISA list of other namespaces) for methods. Applying ref to the variable returns the namespace tag instead of one of the perl types.

    Tie magically connects a variable to one of a special family of modules. That family is distinguished by having the special tie methods like TIEARRAY(), FETCH(), and STORE() defined. The tied variable has value semantics rather than reference semantics.

    The bless function is more fundamental. You'll find it inside each TIE*() constructor.

    After Compline,
    Zaxo

Re: Difference between tie and bless
by jdporter (Paladin) on May 31, 2005 at 16:25 UTC

    The previous two replies are correct, but I believe it is also useful to observe that tie does "normal" OO, in the following sense: perl has intrinsic types — scalar, array, hash, filehandle — each with its proper interface which is exposed in the syntax of perl language. Sometimes we'd like to have a variable which "is" a hash (for example) but which has non-standard behavior. That is, in OO terms, we'd like to sub-class the hash type. tie is what gives us that ability. It just so happens that the implementation of the tie feature uses blessage, but that's a mere detail. It didn't have to be that way. (Of course, it is that way because it makes sense!)

    So, looking at it from the other side, we can say that tie is how we get an arbitrarily complex object (typically in the form of a regular blessed object) to appear in the program as a variable. I would say that the only real limitation is that the tied (blessed) object has to implement the various methods of the base variable type — e.g. FETCH, STORE, etc. — but I'd be wrong. It only has to implement those bits of interface that actually get used in the program. (Perl, being so highly dynamic, is different from other languages in this.) So, for example, if your object supports the concept of accessing individual data elements by index, but not iterating over all such elements, your tied interface could implement the STORE and FETCH methods but not FIRST and NEXT.