Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Confused by OO & tie

by Scarborough (Hermit)
on May 17, 2004 at 15:43 UTC ( #354006=perlquestion: print w/replies, xml ) Need Help??

Scarborough has asked for the wisdom of the Perl Monks concerning the following question:

I have read and tryed the examples in Object-Oriented Tutorial and coming from a Java background found it very understandable, ten out of ten, thanks.

However I had a feeling that tie had something to do with OO so off I went to http://www.perldoc.com/perl5.8.4/pod/func/tie.html and confusion sets in.

The docs say "A class implementing a hash should have the following methods:
TIEHASH classname, LIST
FETCH this, key
STORE this, key, value
DELETE this, key
CLEAR this
EXISTS this, key
FIRSTKEY this
NEXTKEY this, lastkey
SCALAR this
DESTROY this
UNTIE this"

I don't see these methods in the tutorial, which is implementing an odject as a hash, and still seems to work correctly.
Are there any monks who can direct me on the path to enlightenment.

Replies are listed 'Best First'.
Re: Confused by OO & tie
by flyingmoose (Priest) on May 17, 2004 at 15:51 UTC
    However I had a feeling that tie had something to do with OO

    You can disregard 'tie' as you learn OO. tie can be used to bind variables to objects so that in-memory variables (hashes for instance) actually do something else behind the scenes. An example might be a hash that really saved everything you wrote to a database, or maybe a hash that relayed all lookups to a remote server.

    How's this work? Well, there is an object behind the scenes that masquerades as the hash... kinda sorta...

    Anyhow, knowledge of tie is not required for any OO implementation, only if you want to, say, make a hash that has special magic behind the scenes. If you are just using a blessed hash reference to hold your object member data, you don't have to worry about tie. In fact, I've never found a need for it ... but if you have an existing block of code that works with a regular hash, the exact same code will work on a tied hash, and that makes tie interesting.

    Also see "perldoc perltie" rather than "perldoc -f tie", as that seems to make a ton of more sense.

      Thanks, I expect I'll be back later to find out more about tie as I have inherited some code that does ties to a type of resource block, unless of course you can guide me to a tie tutorial or resource a little more in depth than the perldocs mentioned in my post.
Re: Confused by OO & tie
by tilly (Archbishop) on May 17, 2004 at 19:58 UTC
    First of all I still stand by my advice at Re: When to use Tie, don't use tie unless Perl's syntactic design choices are getting in your way.

    That said, the connection between tie and OO is very simple. Provide the right OO interface for Perl to talk to, and Perl will let people use the tie mechanism to make a (variable, array, hash, whatever) be implemented through that interface.

    If that comment confuses, then the example at RE (2): Filehandle Filter may enlighten. Just alias the methods that Perl is looking for to methods that a human would think of, and suddenly what was a tie interface becomes a reasonable object to use directly with OO.

Re: Confused by OO & tie
by davido (Cardinal) on May 17, 2004 at 15:58 UTC
    tie, and perltie are two docs that discuss how to tie variables to objects. But think of it exactly that way; you have an object that you're tieing a variable to.

    That means that Object Oriented Programming doesn't necessarily mean using tie. tie is just one use of OO.

    While tieing variables and filehandles can be a pretty neat trick, it doesn't necessarily lend itself to readability. Most people seeing $var = 10; expect it to do just that; assign ten to the variable $var. With tie, it could actually mean print "Fooled you! Ha!\n";. So keep in mind that if you do use tie, you may make your code harder to read.


    Dave

Re: Confused by OO & tie
by hardburn (Abbot) on May 17, 2004 at 15:59 UTC

    tie does have something to do with OO, but in a far more abstract sense. In OO, polymorphism allows you to treat different things with perhaps wildely different implementations as if they were the same. tie lets you treat these different things as if they were just another variable. In a way, it's the best object system ever. However, it is also a rather limited object system (and slow, but that doesn't matter from a purely-theoretical point of view).

    The tutorial you point to is speaking of Perl's bless-based object system. It's a very flexible system, but it's not the only object system Perl has, and may not even be the best one. It is, however, the most widely-used of Perl's object systems (tie is probably a distant second). Some of these systems might build on bless, and others may use something completely different.

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Confused by OO & tie
by iburrell (Chaplain) on May 17, 2004 at 15:55 UTC
    Think of 'tie' as a way to have an object act like a hash. Perl calls those methods on the object to implement the hash functionality.
    my $value = $hash{$key}; my $object = tied %hash; $value = $object->FETCH($key);
    Most objects are blessed references to hashes; they are implemented as hashes internally. Perl does not have a mechanism for having named attributes (ie instance variables) of an object. People use hashes to store the internal state of the object.

    It is quite possible to have a tied hash implemented using a hash reference object.

Re: Confused by OO & tie
by EdwardG (Vicar) on May 17, 2004 at 15:58 UTC

    The tutorial is using a hash to implement a class.

    The tie docs are talking about implementing a class that can serve as a hash.

    These are not the same thing, don't confuse them.

     

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://354006]
Approved by EdwardG
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-02-09 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (44 votes). Check out past polls.

    Notices?