perltiebless

What is difference between bless and tie in perl?


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


Solution

  • bless is used to construct an object.

    tie allows 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. tie and other forms of magic are primarily used to provide "clever" interfaces (e.g. altering %ENV alters the environment, altering %SIG sets signal handlers, $! mirrors both errno and perror, 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).