Class::Accessor::Fast::Contained - Fast accessors with data containment


Class-Accessor-Fast-Contained documentation Contained in the Class-Accessor-Fast-Contained distribution.

Index


Code Index:

NAME

Top

Class::Accessor::Fast::Contained - Fast accessors with data containment

VERSION

Top

This document refers to version 1.01 of Class::Accessor::Fast::Contained

SYNOPSIS

Top

 package Foo;
 use base qw(Class::Accessor::Fast::Contained);

 # The rest is the same as Class::Accessor::Fast

DESCRIPTION

Top

This module does two things differently to the venerable Class::Accessor::Fast :

DEPENDENCIES

Top

Other than the standard Perl distribution, you will need the following:

BUGS

Top

If you spot a bug or are experiencing difficulties that are not explained within the documentation, please send an email to oliver@cpan.org or submit a bug to the RT system (http://rt.cpan.org/). It would help greatly if you are able to pinpoint problems or even supply a patch.

SEE ALSO

Top

Class::Accessor

AUTHOR

Top

Oliver Gorwits <oliver.gorwits@oucs.ox.ac.uk>

ACKNOWLEDGEMENTS

Top

Thanks to Marty Pauly and Michael G Schwern for Class::Accessor and its tests, which I've shamelessly borrowed for this distribution.

COPYRIGHT & LICENSE

Top


Class-Accessor-Fast-Contained documentation Contained in the Class-Accessor-Fast-Contained distribution.

package Class::Accessor::Fast::Contained;

use strict;
use warnings FATAL => qw(all);

use base qw(Class::Accessor::Fast);

our $VERSION = '1.01';
$VERSION = eval $VERSION; # numify for warning-free dev releases

use Symbol;

# this module does two things differently to the venerable
# Class::Accessor::Fast,
#  1) fields are stored at arms-length in a single key of $self
#  2) new() allows mixin into an existing object

sub new {
    my ($class, $fields) = @_;

    $fields = {} unless defined $fields;

    my $self = (ref $class ? $class : bless {}, $class);

    my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
    $copy->{ref $self} = {%$fields};

    return $self;
}

*{Symbol::qualify_to_ref('setup')} = \&new;

sub make_accessor {
    my($class, $field) = @_;

    return sub {
        my $self = shift;
        my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
        return $copy->{ref $self}->{$field} if scalar @_ == 0;
        $copy->{ref $self}->{$field} = (@_ == 1 ? $_[0] : [@_]);
    };
}


sub make_ro_accessor {
    my($class, $field) = @_;

    return sub {
        my $self = shift;
        my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);
        return $copy->{ref $self}->{$field} if scalar @_ == 0;
        my $caller = caller;
        $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
    };
}

sub make_wo_accessor {
    my($class, $field) = @_;

    return sub {
        my $self = shift;
        my $copy = ("$self" =~ m/=GLOB/ ? *$self : $self);

        unless (@_) {
            my $caller = caller;
            $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
        }
        else {
            return $copy->{ref $self}->{$field} = (@_ == 1 ? $_[0] : [@_]);
        }
    };
}

1;