Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perl OOP

by gmpassos (Priest)
on Oct 11, 2004 at 22:15 UTC ( [id://398315]=note: print w/replies, xml ) Need Help??


in reply to Perl OOP

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".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://398315]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-19 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found