Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
When variables behave differently - Tie in Perl

Deep in the bowels of any programming language is code to store information into a variable, and get information back from that variable. And in a language that's dynamic in its memory allocation, there will also be code to construct and destroy variables.

Does that sound a bit "object oriented". Intentionally so. And in Perl, you can actually get at the four pieces of code methods used and replace them for individual variables if you wish to do so - thus creating your own variables which behave in a different way to default.

Here's a program that appears to overwrite a variable 12 times ...

  for ($k=0;$k<13;$k++) {
    $hand = $pack[$k];

And then appears to go into an infinite loop printing out the last item stored:

  while ($thiscard = $hand) {
    print "playing card $thiscard\n";

It doesn't actually work like that, though, in my example - complete source [here] - because of two extra lines I have added:

  use stack;
  tie $hand,"stack";

These two lines have replaced the standard behaviour of a scalar for the variable $hand - and in the file stack.pm I have defined a new constructor that sets up a list. In the "setter" I have stored each new value onto the end of that list, and in the "getter" I have removed an item from the end of the list. Thus making the variable $hand into a new way of handling a stack.

By replacing the pop with shift in stack.pm, I could turn the tie class into a queue variable - first in, first out, return flas when the queue is empty. This sort of thing is very clever, but you need to consider whether or not anyone who has to maintain the code late on will understand it.

Tieing can also be used on a scalar to make a variable persist from one run of a program to the next (by the simple means of reading and writing to a text file) - see [here] for the call and [here] for the definition. And tieing can be used for lists and hashes too. With a list, you could treat each line of a file as a member of a list ... with a hash you could use a unique key in a database table to be the hash key - so make your database selects, inserts and updates transparent to your code user.

You'll find a number of examples in the resources for our tieing module which we sometimes teach on custom courses (I covered it on one of last week's Perl courses) and I'll cover it on public Perl for Larger Projects courses - often at an "overview" level but also happy to go into some more detail is it's of specific use to any of the delegates. But if you're thinking of using tieing in a piece of production code - please consider maintainability and efficiency before you commit. Both of these concerns can be answered, but it's best to answer them early on and be aware of what you're doing.
(written 2011-08-28)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P304 - Perl - Tieing
  [2243] Changing a variable behaviour in Perl - tieing - (2009-06-16)
  [2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)

Back to
Journey home by public transport for a Bank Holiday
Previous and next
Horse's mouth home
Forward to
A review of the Summer Sunday extra trains on the TransWilts line
Some other Articles
If its Sunday, must it be Weymouth?
Handling binary data in Perl is easy!
Single and double quotes strings in Perl - what is the difference?
A review of the Summer Sunday extra trains on the TransWilts line
When variables behave differently - Tie in Perl
Journey home by public transport for a Bank Holiday
Perl - a quick reminder and revision. Test yourself!
Not multidimentional arrays - but lists of lists. Much more flexible. Perl!
Where do businessmen stay in Melksham?
Buses on the Cambridge Guided Busway
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3409_Whe ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb