What can make you to understand better is to know the meaning of the code {}.

{} means a reference to an anonoymous HASH. Is the same thing of the code above, but without give a name to the hash:

# this... my $self = {} ; # ... is the same of: my %foo ; my $self = \%foo ;
Other thing that you should know is that each time that you make \%foo or {} we will point to a new HASH. So, each time that we go inside the sub new(), a new %foo will be created (since %foo is local due my). Or each time that we execute {} we have a reference to a new anonymous HASH. We can see that above:
my @hold_ref ; for(1..3) { my $hash_ref = {} ; push(@hold_ref , $hash_ref) ; print "$hash_ref\n" ; }
Output:
HASH(0x1a7f10c) HASH(0x1a7f130) HASH(0x1a75468)
Note that the array @hold_ref is just to ensure that we won't have new HASHES created in the same address of old hashes already cleanned from the memory.

So, bless() will always bless a new reference, what means that we have a new object (well this is why we call by the name new() or create()). The meaning of bless is to say that some reference belong to a package/class, and with that we can do a lot of things, and one of them is to call a method, that is the basic of Object Orientation.

Other question is "Why Perl does OO with a reference to some data structure that belongs to a package?". Well, the main purpose of OO is to have a data structure related to the methods that will interact/change this data. Well, actually OO borned in this way, but they forgot to tell this to us.

Graciliano M. P.
"Creativity is the expression of the liberty".


In reply to Re: Perl OOP by gmpassos
in thread Perl OOP by newbio

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.