Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

what's difference between bless and tie?

by Anonymous Monk
on Dec 23, 2012 at 13:25 UTC ( [id://1010091]=perlquestion: print w/replies, xml ) Need Help??

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

bless will bind a data structure to a package name; When I read tie, it seems also bind a data to something. can someone please explain to me their difference? Thanks!
  • Comment on what's difference between bless and tie?

Replies are listed 'Best First'.
Re: what's difference between bless and tie?
by moritz (Cardinal) on Dec 23, 2012 at 13:32 UTC
      "bless allows you to calls methods on the blessed object." , and "the blessed object" actually a data structure saying a hash and bless tells Perl how the hash can be used(methods defined in package). I still feel they're hard to distinguish in their very initial goal.
Re: what's difference between bless and tie?
by BrowserUk (Patriarch) on Dec 23, 2012 at 13:37 UTC

    With tie you bless a named variable, and a predefined set of methods for that variable type are called automatically as the variable is used.

    It allows the variables to be used exactly as if they were normal Perl variables of that type rather than requiring the methods be called using object syntax, which makes for very natural usage:

    tie my @array, 'Some::Thing'; ... $array[ $n ] *= 5;

    Rather than the clumsy:

    my $myObj = Some::Thing->new(); ... $myObj->setItem( $n, $myObj->getItem( $n ) * 5 );

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: what's difference between bless and tie?
by ww (Archbishop) on Dec 23, 2012 at 13:29 UTC
    Did you read perldoc -f bless and perldoc -f tie?

    The documents (as usual) have a few hints.

Re: what's difference between bless and tie?
by LanX (Saint) on Dec 23, 2012 at 21:15 UTC

      Thank you all for the wonderful reply. All replies are what I wanted: concise and by analogy. Sorry for late thanks as the New Year holiday. Frankly to say I didn't catch the answers all yet. But I'll read more as LanX suggested and then read your words again before any further question. Thanks and Happay new year!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found