Fields

fields(3p) Perl Programmers Reference Guide fields(3p)

Fields

fields(3p) Perl Programmers Reference Guide fields(3p) # fields(3p) Perl Programmers Reference Guide fields(3p) NNAAMMEE # fields - compile-time class fields SSYYNNOOPPSSIISS # { package Foo; use fields qw(foo bar _Foo_private); sub new { my Foo $self = shift; unless (ref $self) { $self = fields::new($self); $self->{_Foo_private} = "this is Foo's secret"; } $self->{foo} = 10; $self->{bar} = 20; return $self; } } my $var = Foo->new; $var->{foo} = 42; # this will generate a run-time error $var->{zap} = 42; # this will generate a compile-time error my Foo $foo = Foo->new; $foo->{zap} = 24; # subclassing { package Bar; use base 'Foo'; use fields qw(baz _Bar_private); # not shared with Foo sub new { my $class = shift; my $self = fields::new($class); $self->SUPER::new(); # init base fields $self->{baz} = 10; # init own fields $self->{_Bar_private} = "this is Bar's secret"; return $self; } } DDEESSCCRRIIPPTTIIOONN # The "fields" pragma enables compile-time and run-time verified class fields. ...