fields::aliased - create aliases for object fields
package MyPackage; use strict; use fields qw($scalar @array %hash); sub new { my $class = shift; my $self = fields::new($class); return $self; } sub mymethod { my MyPackage $self = shift; use fields::aliased qw($self $scalar @array %hash); $scalar = 1; @array = (2 .. 4); %hash = ('one' => 1, 'two' => 2); }
This module is a companion to the fields module, which allows efficient handling of instance variables with checking at compile time. It goes one step further and actually creates lexical aliases to the instance values, which can make code not only easier to type, but easier to read as well.
You declare the fields using the fields pragma, as always.
use fields qw($scalar @array %hash nosigil);
Each field name may be preceded by a type sigil to indicate which kind of variable it is. Names without the type sigil are treated as scalars.
For names beginning with an underscore, see "PRIVATE FIELDS" below.
You call fields::new to create the object.
my $self = fields::new($class);
In each method that uses the individual fields, you add a line similar to the following:
use fields::aliased qw($self $scalar @array %hash nosigil);
That is, list the variable being used for the object reference, and then the names of the fields that you are going to use in this method. fields::aliased takes care of declaring the appropriate Perl lexical variables and linking them to the appropriate field. You only need to specify the fields you are actually going to use, including any inherited from superclasses.
fields::aliased
The fields pragma supports a means of declaring fields that are not available to subclasses: by prefixing them with an underscore character. This module supports that convention (actually, it has no choice!).
use fields qw(_$private_scalar _@private_array _%private_hash);
Note that the underscore goes before the type sigil; this is so that fields gets things right. However, the variable name has the sigil at the front, as always. Thus a field named _$private_scalar is linked to a variable named $_private_scalar. A field named _private, of course, is linked to a variable named $_private.
_$private_scalar
$_private_scalar
_private
$_private
In Perl 5.9.1, using private fields doesn't seem to be working at all. This is due to the switch to restricted hashes vs. pseudohashes, but I don't have all the issues figured out yet.
Initialize field values at use fields::aliased time rather than when the object is created. Net effect should be identical, but this allows private fields to work.
use fields::aliased
It doesn't appear to be possible to initialize private fields in a superclass in a generic initialization method. So now we skip that and throw the responsibility back on the programmer.
Many changes to make private fields in superclasses work.
Added find_funcv to .xs code.
Fix distribution.
Original version.
fields, Perl6::Binding, Lexical::Alias
Tie::IxHash, Filter::Util::Call, Test::More
Copyright 2004 Kevin Michael Vail
This program is free software. It may be copied and/or redistributed under the same terms as Perl itself.
Kevin Michael Vail <kvail@cpan.org>
To install fields::aliased, copy and paste the appropriate command in to your terminal.
cpanm
cpanm fields::aliased
CPAN shell
perl -MCPAN -e shell install fields::aliased
For more information on module installation, please visit the detailed CPAN module installation guide.