Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the common idea of perl modules with Tie:: namespace?

Tags:

naming

perl

tie

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?

like image 337
Paul Serikov Avatar asked Dec 13 '22 12:12

Paul Serikov


2 Answers

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.
like image 88
ikegami Avatar answered Jan 04 '23 23:01

ikegami


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

like image 34
Dave Cross Avatar answered Jan 05 '23 00:01

Dave Cross