4

There are a lot of modules with Tie::namespace on CPAN - Tie::Hash, Tie::Sub, Tie::Cache, Tie::DBI, etc. What is common among them ?

I checked perltie but 'm not sure that I understood the concept clear. Could someone explain it?

Paul Serikov
  • 2,550
  • 2
  • 21
  • 36
  • It is more or less to bind a perl structure in memory directly to a file. So when your script/application closes and starts again it can excess the prior values. – smartmeta Feb 13 '19 at 09:06
  • @smartmeta That's only a small subset of what the `tie` interface can do. One of the oldest, certainly, but the `tie` interface is a general-purpose interface that allows tying most kinds of Perl variables to any logic one wants. – haukex Feb 13 '19 at 18:31

2 Answers2

9

The modules in the Tie:: namespace fall into two categories:

  • Those that implement a class to which a variable can be tied (e.g. Tie::DBI), and
  • Those that assist the user in building such classes (e.g. Tie::Hash).

tie allows an object to take on the interface of a variable. When you read from a tied variable, you are actually calling a method to retrieve information. When you write to a tied variable, you are actually calling a method to with the information.

For example, let's look at a hash tied to Tie::DBI.

  • When you list the keys of the hash, a method is called which obtains the primary key of each row of a database table instead.
  • When you fetch the value of an element of the hash, a method is called which gets the specified row of a database table instead.
  • When you create/set the value of an element of the hash, a method is called which creates/set the fields of the specified row of a database table instead.
ikegami
  • 367,544
  • 15
  • 269
  • 518
3

It only covers tying hashes, but my article on perl.com from 2001 might answer a few questions.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97