NAME

fields::aliased - create aliases for object fields

SYNOPSIS

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);
}

DESCRIPTION

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.

Declarations

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.

Constructors

You call fields::new to create the object.

my $self = fields::new($class);

Usage

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.

PRIVATE FIELDS

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.

KNOWN PROBLEMS

  • 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.

HISTORY

1.05

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.

1.04

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.

1.03

Many changes to make private fields in superclasses work.

1.02

Added find_funcv to .xs code.

1.01

Fix distribution.

1.00

Original version.

SEE ALSO

fields, Perl6::Binding, Lexical::Alias

REQUIRED MODULES

Tie::IxHash, Filter::Util::Call, Test::More

COPYRIGHT AND LICENSE

Copyright 2004 Kevin Michael Vail

This program is free software. It may be copied and/or redistributed under the same terms as Perl itself.

AUTHOR

Kevin Michael Vail <kvail@cpan.org>