Chapter 14. Tied Variables
Some human endeavors require a disguise. Sometimes the intent is
to deceive, but more often the intent is to communicate something true
at a deeper level. For instance, many job interviewers expect you to
dress up in a tie to indicate that you’re seriously interested in
fitting in, even though both of you know you’ll never wear a tie on the
job. It’s odd when you think about it: tying a piece of cloth around
your neck can magically get you a job. In Perl culture, the tie operator plays a
similar role: it lets you create a seemingly normal variable that,
behind the disguise, is actually a full-fledged Perl object that is
expected to have an interesting personality of its own. It’s just an odd
bit of magic, like pulling Bugs Bunny out of a hat.
Put another way, the funny characters $, @,
%, or * in front of a variable name tell Perl and
its programmers a great deal—they each imply a particular set of
archetypal behaviors. You can warp those behaviors in various useful
ways with tie, by associating the
variable with a class that implements a new set of behaviors. For
instance, you can create a regular Perl hash, and then tie it to a class that makes the hash into a database, so that when you read values from the hash, Perl magically fetches data from an external database file, and when you set values in the hash, Perl magically stores data in the external database file. In this case, “magically” means “transparently doing something very complicated”. You know ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access