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);
    fields::aliased::init($self);

    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, and then call fields::aliased::init to set the fields to suitable initial values.

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

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.

Important Note

Because of the way use fields works, it is not possible for fields::aliased::init to initialize private variables in superclasses, so it skips the initialization for all field names beginning with an underscore. Therefore, you are responsible for initializing these values yourself. For a scalar field, this works out all right anyway, because the initial value of a hash or array element that's never been assigned a value is undef, which is what this module would have assigned anyway. However, any private field declared as an array or hash must be set to an appropriate reference during object construction if you expect to use it in a use fields statement within a method, or you will get a run-time error.

FUNCTIONS

fields::aliased::init

fields::aliased::init($self);

This function sets all of the fields in $self, including inherited fields (but not including private fields, see above), to suitable defaults. It should be called right after fields::new.

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