Thread Module (28 answers)
Opened by Strazke at 2005-09-14 01:27

Strazke
 2005-09-14 01:27
#57877 #57877
User since
2005-07-11
120 Artikel
BenutzerIn
[default_avatar]
Hallo, ich habe hier(http://www.troubleshooters.com/codecorn/littperl/perloop.htm) eine Anleitung zum Objektorientierten Programmieren mit Perl gefunden. Dabei verstehe ich aber (teils auch wegen meinem chlechtem Englisch ;) ) manche sachen nicht. Ich hoffe Ihr könnt mir helfen:

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sub new
{
#Arg0 is the type because the constructor will look like
# my($instance) = Tree->new(arg1,arg2,whatever)
#so arg0 will be Tree.
my($type) = $_[0];

#Make subroutine-local var $self, and make it a reference.
#Specifically, make it a reference to a (right now) empty hash.
#Later on, that hash will contain object properties.
my($self) = {};

#For now, we'll have one instance variable (property, whatever)
#It will be in the hash referenced by $self, and will have
#the index 'root'. This will be the first arg (inside the parentheses)
#of the call to the constructor in the main program.
$self->{'root'} = $_[1]; #remember $_[0] was the Tree before the ->

#There's nothing reserved about the word $self. It could have been
#called $oodolaboodola. To link the object with both the hash pointed
#to by $self and the type (Tree), we use the 2 argument version
#of the keyword bless:
bless($self, $type);

#Now finally, return the hash as a reference to be used as an "object"
return($self);
}


Ok, hier verstehe ich nicht, wozu man überhaupt einen Konstruktor braucht. Ist es nicht viel einfacher, einfach eine Funktion zu schreiben, die das gewünschte zurüchgibt? Und mal sehen ob ich das richtig verstanden habe: Es wird ein Hash erstellt, das dann Tree zugeordnet wird(wieso das eigentlich?) und dann wird es zurückgegeben.

Stimmt das so weit?

View full thread Module