in reply to Perl OOP
{} means a reference to an anonoymous HASH. Is the same thing of the code above, but without give a name to the hash:
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:# this... my $self = {} ; # ... is the same of: my %foo ; my $self = \%foo ;
Output:my @hold_ref ; for(1..3) { my $hash_ref = {} ; push(@hold_ref , $hash_ref) ; print "$hash_ref\n" ; }
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.HASH(0x1a7f10c) HASH(0x1a7f130) HASH(0x1a75468)
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".
|
|---|