Rob Richardson wrote: > Greetings! > > I am beginning to seriously consider what I am going to have to do to > wrestle some reasonable amount of organization out of the unbelievable > pile of spaghetti I've been trying to update. I managed to add what I > needed; now I'm going to go back and do it the way it should have been > done, mainly as an academic exercise. > > The new version of my script will be based on objects. So, I was > reading the Camel Book (2nd ed) about objects. It started talking > about "tie", and the process of tying variables to classes. It went > into some detail about how to do it, but it didn't say a durn thing > about why o do it. > > Why would I want to "tie" a variable, and what actually happens when > a variable is tied?
Tying a variable allows you to write code to handle the normal operations against that variable. The classic case of this is tying a hash to an external file or database, so you can write the normal: $hash{key} = "value"; and have code that automatically stores that value out to a database or file. Here's a real simple example with a scalar: #!/usr/bin/perl package Timestamp; use base qw/Tie::Scalar/; sub TIESCALAR { my $self; bless \$self, shift } sub FETCH { localtime } package main; tie $now, 'Timestamp'; print "The time is $now\n"; sleep 5; print "Now it's $now\n"; The tied variable $now will evaluate to a timestamp string whenever you use it. This is because the interpolation of the variable in the print statement (it could also have been a normal assignment or other expression) causes the FETCH method of the class Timestamp to be called. So $now is a "magic" variable that always evaluates to the current timestamp. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]