2

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

Kamal Nayan
  • 1,890
  • 21
  • 34
  • 2
    I don't think I would want to work in a place that asks this kind of interview question. – simbabque Dec 21 '16 at 08:35
  • I am a perl developer and using it in day to day life. So my concern is not for interview, but for knowing it. – Kamal Nayan Dec 21 '16 at 08:37
  • 1
    DId you read http://perldoc.perl.org/perltie.html as well? – simbabque Dec 21 '16 at 08:40
  • fortunately I was just reading the same doc and this link too `http://www.perlmonks.org/bare/?node_id=461198` – Kamal Nayan Dec 21 '16 at 08:41
  • 1
    That's not very detailed about `tie`. Some example uses of `tie` are to use an array for a file, where each row represents a line in a file, and if you change array elements, stuff magically happens on disk, or tying a hash to some function so you can call `$tied_hash{$arg}` and it will call a function with `$arg` under the hood. But I've never built anything with `tie` myself. – simbabque Dec 21 '16 at 08:55
  • Here is an abstract similarity. A `tie` associates (or, equips) a variable with some behavior, action. So when you just read a tied scalar, things happen -- anything you code. Perhaps a database entry is made, or a text message sent. The `bless` associates a variable (reference) with a package. So now when you use that reference (an object) anything you coded may happen. So both associate a variable with actions. – zdim Dec 21 '16 at 09:57
  • `bless` allows you to calls methods on the blessed object. `tie` allows you to override built-in operations on variables, namely reading from/assigning to scalars, and list and hash operations like indexing, exists, delete etc. So in some sense, `tie` works on a variable and `bless` works on a thing that is stored in a variable. – ssr1012 Dec 21 '16 at 11:18
  • tie is an interesting one - it lets you do all sorts of clever things, but I'm not honestly sure it's good style to make a 'standard' variable do funky things like that, because it's action at a distance. Future programmers might expect `$funky_tied_var` to behave in a way that it doesn't. But for testing, it can be really pretty handy to have 'magic' going on. – Sobrique Dec 21 '16 at 13:04

2 Answers2

4

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).

ikegami
  • 367,544
  • 15
  • 269
  • 518
3

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.

Sobrique
  • 52,974
  • 7
  • 60
  • 101