1
2
3
4
5
6
sub new {
my $class = shift @_;
my $self = { @_ };
bless $self, $class;
return $self;
}
my $obj = MyClass->new(message => "Guten Morgen", nachname => "Wall", vorname => "Larry");
1
2
3
4
5
6
7
8
9
10
MyClass {
public methods (3) : add, greet, new
private methods (0)
internals: {
message "Hello",
nachname "Wall",
name "nachname",
vorname "Larry"
}
}
1
2
3
4
5
6
7
8
9
sub add {
my $self = shift;
$self->{@_} = { @_ };
$self->{$_[0]} = { @_ };
if (@_) {
$self->{ID} = shift(@_);
}
return $self->{@_};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MyClass {
public methods (3) : add, greet, new
private methods (0)
internals: {
4 {
bla "blubb",
foo "bar"
},
foo {
bla "blubb",
foo "bar"
},
ID "foo",
message "Guten Morgen",
nachname "Wall",
vorname "Larry"
}
}
1
2
3
4
5
6
7
8
9
10
11
MyClass {
public methods (3) : add, greet, new
private methods (0)
internals: {
bla "blubb",
foo "bar",
message "Guten Morgen",
nachname "Wall",
vorname "Larry"
}
}
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 28 29 30
package MyClass; sub new { my ($class, %args) = @_; return bless \%args => $class; } sub add { my ($self, %args) = @_; while (my ($k, $v) = each %args) { $self->{$k} = $v; } return $self; } sub greet {} package main; my $obj = MyClass->new(message => "Guten Morgen", nachname => "Wall", vorname => "Larry"); $obj->add(bla => 'blubb', foo => 'bar'); use DDP; p $obj; __END__ MyClass { public methods (3) : add, greet, new private methods (0) internals: { bla "blubb", foo "bar", message "Guten Morgen", nachname "Wall", vorname "Larry" } }
return bless \%args => $class;
$args{key} = "value"
%args = qw(key value).
return(bless(\%args => $class));
Quotebless auf den benannten Hash %args angewendet wird
Quoteeinem %Hash als ganzes einen Skalar zuweisen
QuoteWird der Hash hier nur "als mittel zum Zweck […] so dass der Name %args verworfen wird?
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 28 29 30 31 32
sub new{ my $class = shift; my %att = ( name => 'Foo', vname => 'Bär', @_); bless{ name => $att{name}, vname => $att{vname}, USER => {} }, $class; } sub add{ my $self = shift; my $att = shift; my $val = shift; $self->{USER}{$att} = $val; } my $m = main->new(name => 'booooo'); $m->add('PLZ','12345'); puts Dumper $m; # das gibt aus: $VAR1 = bless( { 'USER' => { 'PLZ' => '12345' }, 'name' => 'booooo', 'vname' => 'Bär' }, 'main' );