Schrift
Wiki:Tipp zum Debugging: use Data::Dumper; local $Data::Dumper::Useqq = 1; print Dumper \@var;
[thread]11010[/thread]

OOP, Hash an den Konstruktor uebergeben

Leser: 2


<< >> 4 Einträge, 1 Seite
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.
pq
 2007-12-12 16:09
#103782 #103782
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ perl -wle'
my %h = (a => 23, b => 24, c => 25);
print "while";
while (my ($k, $v) = each %h) {
print "$k => $v";
}
print "for";
for (my ($k, $v) = each %h) {
print "$k => $v";
}
'
while
c => 25
a => 23
b => 24
for
c => 25
c => 25
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
renee
 2007-12-12 16:14
#103784 #103784
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Du musst statt dem for ein while nehmen!
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
styx-cc
 2007-12-12 17:54
#103794 #103794
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Das haette ich so schnell nicht rausgefunden...
Vielen Dank euch!
Pörl.
<< >> 4 Einträge, 1 Seite



View all threads created 2007-12-12 15:50.