I was practicing some interview questions where I found this. I had a look at perldoc -f tie but couldn't get it. I know about bless and is using in my programs.
 
Can anyone tell me what is tie and how it is related and different from bless, and its usage? I will appreciate any simple example.
PS: I am a perl developer and using it in day to day life. My concern is not for interview, but for knowing it
What is difference between bless and tie in perl?
613 Views Asked by Kamal Nayan AtThere are 2 best solutions below
                        
                            
                        
                        
                            On
                            
                                                    
                    
                I would probably answer your interview question like this:
Tie and bless serve similar functions, in that they associate custom behaviour with a variable. The difference comes in terms of focus - tie is about adding new behaviour to a 'standard' data structure, where bless is about creating a 'new' data structure altogether. 
You are therefore more limited with tie to the fundamental operations of the data type you're working with, where a blessed data structure has an almost unlimited scope of possiblity. But the tradeoff is that tieed variables can serve as drop in replacements - if you extend a 'hash' by tieing it to a file or a database, it can still function in the same way without any sort of code revision needed. 
This strength is also in many ways it's weakness though - where a future programmer when confronted with an object and a method call in some code, would know they need to look to the class to understand what's going on - they might not realise that a tied hash (or scalar) is doing 'something special'.
I would therefore suggest that as a matter of style tie should be reserved for a diagnostic and testing role - it may be handy to log when a value changes in a hash, but shouldn't be used to make things behave in unexpected ways. 
blessis used to construct an object.tieallows code to provide a variable for interface. e.g. It might look like you are assigning to a variable, but you are really calling a sub.tieand other forms of magic are primarily used to provide "clever" interfaces (e.g. altering%ENValters the environment, altering%SIGsets signal handlers,$!mirrors botherrnoandperror, etc). It can also be used to extend the usefulness of existing code (e.g. creating something that looks like a file handle allows one to reuse code that expects to get its input from a file handle).