31.9. use fieldsIn the Pet module: In a separate program, demopet:package Pet; use strict; use fields qw(name weight _Pet_pid); my $PID = 0; sub new { my Pet $self = shift; unless (ref $self) { $self = fields::new($self); $self->{_Pet_pid} = "this is Pet's secret ID"; } $self->{name} = "Hey, you!"; $self->{weight} = 20; return $self; } 1; In the Dog module:use Pet; my Pet $rock = new Pet; # typed lexical $rock->{name} = "quartz"; $rock->{weight} = "2kg"; $rock->{_Pet_pid} = 1233; # private attribute $rock->{color} = "blue"; # generates compile-time error In a separate program, demodog:package Dog; use strict; use base 'Pet'; # inherit fields and methods from Pet use fields qw(name pedigree); # override Pet name attribute, # add new pedigree attribute use fields qw(wag _Dog_private); # not shared with Pet sub new { my $class = shift; my $self = fields::new($class); $self->SUPER::new(); # init base fields $self->{pedigree} = "none"; # init own fields return $self; } use Dog; my Dog $spot = new Dog; # typed lexical $spot->{name} = "Theloneus"; # not inherited $spot->{weight} = "30lbs"; # inherited $spot->{pedigree} = "mutt"; # not inherited $spot->{color} = "brown"; # generates compile-time error $spot->{_Pet_pid} = 3324; # generates compile-time error The fields pragma provides a method of declaring class fields that can be type checked at compile time. This relies on a feature known as pseudohashes: if a typed lexical variable (my Pet $rock) is holding a reference (the Pet object) and is used to access a hash element ($rock->{name}), and if there exists a package with the same name as the declared type that has set up class fields using the fields pragma, then the operation is turned into an array access at compile time, provided the field specified is valid. 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. Field names that start with an underscore character are made private to the class and are not visible to subclasses. Inherited fields can be overridden but will generate a warning if warnings are enabled. The effect of all this is that you can have objects with named fields which are as compact as arrays and as fast to access. This only works as long as the objects are accessed through properly typed lexical variables, though. If the variables are not typed, access is only checked at run time, so your program runs slower because it has to do both a hash access and an array access. In addition to field declarations, the following functions are supported:
If the first argument to phash is a reference to an array, the pseudohash 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 less elements than the first, the trailing elements of the pseudohash will not be initialized. This makes it particularly useful for creating a pseudohash from subroutine arguments: Alternatively, you can pass a list key/value pairs that will be used to construct the pseudohash:sub dogtag { my $tag = fields::phash([qw(name rank ser_num)], [@_]); } For more on pseudohashes, see the section "Pseudohashes" in Chapter 8, "References".my $tag = fields::phash(name => "Joe", rank => "captain", ser_num => 42); my $pseudohash = fields::phash(%args); The current implementation keeps the declared fields in the %FIELDS hash of the calling package, but this may change in future versions, so it's best to rely on this pragma's interface to manage your fields. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|