Thread OOP, Hash an den Konstruktor uebergeben (3 answers)
Opened by styx-cc at 2007-12-12 15:50

styx-cc
 2007-12-12 15:50
#103780 #103780
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Holla.
Ich versuche gerade einen Konstruktor zu schrieben, welchem man beim Aufruf gleich ein paar Parameter mitgeben kann:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package Stats;
use strict;
use warnings;
use Carp qw/croak/;

sub new {
ref (my $class = shift) && croak 'class name needed in constructor';
my $self = {@_};
bless $self, $class;
}

sub alive {
my $self = shift;
for (my ($k, $v) = each(%$self) ) {
print "$k has the value $v\n";
}
}
1;


So rufe ich das auf:
Code: (dl )
perl -MStats -e '$x = Stats->new(-test => 'bla', -test2 => 'blubb'); $x->alive();'


Die Ausgabe:
Code: (dl )
1
2
-test2 has the value blubb
-test2 has the value blubb


Jetzt frage ich mich, was er mit -test gemacht hat!?
Ich hatte eher mit der Ausgabe
Code: (dl )
1
2
-test has the value bla
-test2 has the value blubb

gerechnet.

Vielen Dank
MfG
Pörl.

View full thread OOP, Hash an den Konstruktor uebergeben