I think it's easiest to see what bless does by constructing an object and method without any module or package. Here's a bare-bones object with a single method.

# a method - get/set the value sub Foo::value () :lvalue { ${$_[0]} } # make three references to two things my ($foo, $bar) = (do { my $v = 1; \$v }) x 2; # 2 refs to same $v my $baz = do { my $v = 1; \$v }; # different $v # . . . and a fourth my $quux = bless $foo, 'Foo'; printf "\$foo\t%s\n\$bar\t%s\n\$baz\t%s\$quux\t%s\n", $foo, $bar, $baz, $quux; print 'value is ', $foo->value(), $/; $bar->value() = 10; print 'now, ', $quux->value(), $/; __END__ $foo Foo=SCALAR(0x80591e8) $bar Foo=SCALAR(0x80591e8) $baz SCALAR(0x8059248) $quux Foo=SCALAR(0x80591e8) value is 1 now, 10

Notice that $bar got blessed, too, though we didn't do a thing to $bar itself. $baz escaped, having its own thing to point to.

So, we see what bless does. It takes a reference and an identifier string, and marks what the reference points to as an object tagged by the identifier. That identifier is used in looking for methods by checking the symbol table for the tag and looking under it for subs.

After Compline,
Zaxo


In reply to Re: why "Bless" in OO Perl by Zaxo
in thread why "Bless" in OO Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.