tie and bless have very similar functions. They both associate some sort of Perl thing, such as a hash or a reference, with some Perl class, such as SDBM_File or CGI. This allows you to access aspects of the class from the tied or blessed thing.

There is a key difference between tie and bless. tie changes the behavior of a standard Perl type. For example, when you tie a hash to the SDBM_File class, from outside the hash looks exactly the same, but behind the scenes the data is stored in a file instead of in memory.

bless, on the other hand, acts on a reference to something. With that reference, you get a whole new interface provided by the class. For example, CGI->new() calls bless and gives you back a CGI object. You can call methods on that object, like param() and header(). (You don't even have to care if the reference refers to a hash or an array or something else.)

Here's an example of tie. See how, after %hash has been tied, you still access it just like a regular hash, even though Everything in the hash is being stored on disk. The file sticks around after the program exits, so you can tie another hash to the same file with SDBM_File and access all that data again.

#!/usr/local/bin/perl -w use strict; use Fcntl; use SDBM_File; my %hash; tie(%hash, 'SDBM_File', 'example', O_RDWR|O_CREAT, 0640); $hash{'animal'} = 'dog'; @hash{'vegetable', 'mineral'} = ('carrot', 'quartz'); print "$hash{'animal'} is an animal.\n"; while (my($key, $val) = each %hash) { print "My $key is a $val.\n"; } __END__
Here's an example of bless. Note that MyObject->new() blesses an anonymous hash reference, shifting the name of the class from @_. In the other methods, MyObject keeps data in the hash. In the main code, however, you don't need to know that MyObject is a hash under the hood.
#!/usr/local/bin/perl -w use strict; package MyObject; sub new { return bless {}, shift; } sub get { my $self = shift; return $self->{$_[0]}; } sub set { my $self = shift; $self->{$_[0]} = $_[1]; } sub greet { print "Hello, world!\n"; } package main; my $obj = new MyObject; $obj->greet(); $obj->set('fruit', 'banana'); print $obj->get('fruit'), " is a fruit.\n"; __END__
That just shows the basics of these functions, which are really quite powerful and make possible lots of cool stuff. I hope this quick introduction was helpful!

In reply to Re: tie and bless - guidance needed by chipmunk
in thread tie and bless - guidence needed by Mac

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.