fields(3perl)
π fields β compile-time class fields π
okay bestie, so this is fields, from the Perl Programmers Reference Guide (the 3perl manual section, if you're keeping score). it's a pragma. it's giving typed object vibes. let's get into it π«Ά
β¨ SYNOPSIS β¨
{
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;
}
}
πΈ DESCRIPTION πΈ
so the fields pragma π
enables compile-time and run-time verified class fields. that's the whole bit. iconic, honestly.
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%FIELDShash directly, because it must be created at compile-time for it to be fully useful, as is done by this pragma. so likeβ¦ don't touch it. hands off. π
okay so here's the moment: if you have a typed lexical variable (my Class $var) holding a reference, and you use it to access a hash element, and a package with the same name as the type has declared class fields using this pragma β then the hash key gets verified at compile time. slay. but if the variables are not typed, access is only checked at run time. so typing your vars really is the main character move here. β¨
the related base pragma will combine fields from base classes and any fields declared using the fields pragma. this enables field inheritance to work properly. inherited fields can be overridden but will generate a warning if warnings are enabled. noted. β οΈ
Only valid for Perl 5.8.x and earlier: field names that start with an underscore character are made private to the class and are not visible to subclasses. so _Foo_private really said "this is mine, not yours." π
also, in Perl 5.8.x and earlier, this pragma uses pseudo-hashes, the effect being that you can have objects with named fields which are as compact and fast as arrays to access β as long as the objects are accessed through properly typed variables. fast and named? she ate. π₯
the following functions are supported:
π new
fields::new() creates and blesses a hash of the fields declared using the fields pragma into the specified class. it is the recommended way to construct a fields-based object. π
this makes it possible to write a constructor like this:
package Critter::Sounds;
use fields qw(cat dog bird);
sub new {
my $self = shift;
$self = fields::new($self) unless ref $self;
$self->{cat} = 'meow'; # scalar element
@$self{'dog','bird'} = ('bark','tweet'); # slice
return $self;
}
π¦ phash
this function only works in Perl 5.8.x and earlier. pseudo-hashes were removed from Perl as of 5.10. consider using restricted hashes orfields::new()instead (which itself uses restricted hashes under 5.10+). seeHash::Util. usingfields::phash()under 5.10 or higher will cause an error. so like⦠fr fr, don't.
fields::phash() can be used to create and initialize a plain (unblessed) pseudo-hash. this function should always be used instead of creating pseudo-hashes directly. no DIY pseudo-hashes, bestie. π«
if the first argument is a reference to an array, the pseudo-hash will be 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 as the values. if the second array contains fewer 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 from subroutine arguments:
sub dogtag {
my $tag = fields::phash([qw(name rank ser_num)], [@_]);
}
fields::phash() also accepts a list of key-value pairs that will be used to construct the pseudo hash. examples:
my $tag = fields::phash(name => "Joe",
rank => "captain",
ser_num => 42);
my $pseudohash = fields::phash(%args);
β SEE ALSO β
base, Hash::Util
and that's the whole page, babe π this was fields(3perl) β Perl v5.44.0, dated 2026-10-17. go forth and type your lexicals π
β¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms