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 (Pope) 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 (Archbishop) 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!