Thread Delphi "Record" in Perl gesucht
(11 answers)
Opened by Mampfgnom at 2011-01-07 13:03 Code (perl): (dl
)
1 2 my $person={Alter=>25, Name=>'Peter', Größe=>172}; print $person->{Alter}."\n" Oder halt Objektorientiert: Klassisch: Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package Person; sub new { my $class=shift; my %opts=@_; my $self={}; $self->{$_}=$opts{$_} for( qw(Alter Name Größe) ) } sub _attr { my $self=shift; my $attr=shift; $self->{$attr}=shift if(@_); return $self->{$attr}; } sub Alter{ shift->_attr('Alter',@_) } sub Name{ shift->_attr('Name',@_) } sub Groesse{ shift->_attr('Groesse',@_) } Aufruf: Code (perl): (dl
)
1 2 my $person=Person->new(Alter=>25, Name=>'Peter', Groesse=>172); print $person->Alter(); Moose Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package Person; use Moose; has 'Name' => ( is => 'rw', isa => 'Str', ); has 'Groesse' => ( is => 'rw', isa => 'Num', ); has 'Alter' => ( is => 'rw', isa => 'Num', ); no Moose; __PACKAGE__->meta->make_immutable; Aufruf wie beim klassischen. |