Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Perl pragma to enable new featuresPerl pragma to control the fil ... (Berikutnya)
Pragmas

Compile-time class fields

Daftar Isi

NAME

fields - compile-time class fields

SYNOPSIS

  1. {
  2. package Foo;
  3. use fields qw(foo bar _Foo_private);
  4. sub new {
  5. my Foo $self = shift;
  6. unless (ref $self) {
  7. $self = fields::new($self);
  8. $self->{_Foo_private} = "this is Foo's secret";
  9. }
  10. $self->{foo} = 10;
  11. $self->{bar} = 20;
  12. return $self;
  13. }
  14. }
  15. my $var = Foo->new;
  16. $var->{foo} = 42;
  17. # this will generate an error
  18. $var->{zap} = 42;
  19. # subclassing
  20. {
  21. package Bar;
  22. use base 'Foo';
  23. use fields qw(baz _Bar_private); # not shared with Foo
  24. sub new {
  25. my $class = shift;
  26. my $self = fields::new($class);
  27. $self->SUPER::new(); # init base fields
  28. $self->{baz} = 10; # init own fields
  29. $self->{_Bar_private} = "this is Bar's secret";
  30. return $self;
  31. }
  32. }

DESCRIPTION

The fields pragma enables compile-time verified class fields.

NOTE: The current implementation keeps the declared fields in the %FIELDShash of the calling package, but this may change in future versions.Do not update the %FIELDS hash directly, because it must be createdat compile-time for it to be fully useful, as is done by this pragma.

Only valid for perl before 5.9.0:

If a typed lexical variable holding a reference is used to access ahash element and a package with the same name as the type hasdeclared class fields using this pragma, then the operation isturned into an array access at compile time.

The related base pragma will combine fields from base classes and anyfields declared using the fields pragma. This enables fieldinheritance to work properly.

Field names that start with an underscore character are made private tothe class and are not visible to subclasses. Inherited fields can beoverridden but will generate a warning if used together with the -wswitch.

Only valid for perls before 5.9.0:

The effect of all this is that you can have objects with namedfields which are as compact and as fast arrays to access. This onlyworks as long as the objects are accessed through properly typedvariables. If the objects are not typed, access is only checked atrun time.

The following functions are supported:

  • new

    perl before 5.9.0: fields::new() creates and blesses apseudo-hash comprised of the fields declared using the fieldspragma into the specified class.

    perl 5.9.0 and higher: fields::new() creates and blesses arestricted-hash comprised of the fields declared using the fieldspragma into the specified class.

    This function is usable with or without pseudo-hashes. It is therecommended way to construct a fields-based object.

    This makes it possible to write a constructor like this:

    1. package Critter::Sounds;
    2. use fields qw(cat dog bird);
    3. sub new {
    4. my $self = shift;
    5. $self = fields::new($self) unless ref $self;
    6. $self->{cat} = 'meow'; # scalar element
    7. @$self{'dog','bird'} = ('bark','tweet'); # slice
    8. return $self;
    9. }
  • phash

    before perl 5.9.0:

    fields::phash() can be used to create and initialize a plain (unblessed)pseudo-hash. This function should always be used instead of creatingpseudo-hashes directly.

    If the first argument is a reference to an array, the pseudo-hash willbe created with keys from that array. If a second argument is supplied,it must also be a reference to an array whose elements will be used asthe values. If the second array contains less elements than the first,the trailing elements of the pseudo-hash will not be initialized.This makes it particularly useful for creating a pseudo-hash fromsubroutine arguments:

    1. sub dogtag {
    2. my $tag = fields::phash([qw(name rank ser_num)], [@_]);
    3. }

    fields::phash() also accepts a list of key-value pairs that willbe used to construct the pseudo hash. Examples:

    1. my $tag = fields::phash(name => "Joe",
    2. rank => "captain",
    3. ser_num => 42);
    4. my $pseudohash = fields::phash(%args);

    perl 5.9.0 and higher:

    Pseudo-hashes have been removed from Perl as of 5.10. Consider usingrestricted hashes or fields::new() instead. Using fields::phash()will cause an error.

SEE ALSO

base

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Perl pragma to enable new featuresPerl pragma to control the fil ... (Berikutnya)