Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Tied Variables - why?

by Revelation (Deacon)
on Sep 07, 2002 at 04:10 UTC ( [id://195830]=note: print w/replies, xml ) Need Help??


in reply to Tied Variables - why?

According to perltie: The tie() function binds a variable to a class (package) that will provide the implementation for access methods for that variable. Once this magic has been performed, accessing a tied variable automatically triggers method calls in the proper class. The complexity of the class is hidden behind magic methods calls. Basically, tie() is used to make a complex system of functions a simple scalar or hash.

What this comes down to, and the reason I use perltie often is because tying to a scalar or a hash is functioning as a way to limit the complexity a user has to deal with, and at times a tied value may just seem intuitive.

A good example in both cases is Apache::Session. The interface to Apache::Session is very simple: tie a hash to the desired class and use the hash as normal. It's much simpler for a user to just modify a hash value, and let Apache::Session do the real work, than it would be otherwise. A hash seems much more intuitive, and simpler to manipulate than an object that may be messed around with, or a user misusing subroutines. There is *no* way for a user to mess up, when using Apache::Session.

Another example would be lots of data manipulation drivers/modules, such as Tie::File, and DB_File. Hash provide organization for our data, and tied hashes just seem to be perfect for retrieval and insertation of data. There are probably countless other examples, as well, where a tied file just *feels* right, and in perl TM..... means we can do things in our own style, which may very well be tie() :)

Hope that helps, but if you still don't believe tied variables are any help, I'd advise you to read this article, if you want a more detailed explanation by a better coder.
Gyan Kapur
gyan.kapur@rhhllp.com

Replies are listed 'Best First'.
Re: Re: Tied Variables - why?
by perrin (Chancellor) on Sep 07, 2002 at 15:26 UTC
    Apache::Session is not a good poster child for the use of tied interfaces. There is a constant stream of questions on the mod_perl and Mason mailing lists from people who are having problems becuase of it. They get bitten by scoping problems, can't figure out how to call methods on the tied hash, or have trouble with deep updates (updating something that is not at the top level of the hash, which fails to call the STORE method in Apache::Session). Many of these problems would just go away if Apache::Session didn't have a tied interface.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://195830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found