Class-Accessor-Ref

 view release on metacpan or  search on metacpan

lib/Class/Accessor/Ref.pm  view on Meta::CPAN

    # Get several references at once
          ($fooref, $barref) = $obj->get_ref(qw/foo bar/);
    #
    # Stringify the reference, not the number "1":
          print "\$obj->{foo} is at " . $obj->get_ref('foo');

=cut

# XXX: This could benefit from memoization, but I don't know if
# I want to add that without asking the users -- if they call this
# on many many objects, it'll just be a waste of space. But adding
# a real LRU cache seems like a bit of an overkill :/

sub get_ref {
	my($self, @fields) = @_;
	my $class = ref $self;
	die "Can't take reference to members of unknown class $class. ".
		"Did you call $class->mk_refaccessors?"
		unless $CLASSES{$class};
	my @refs;
	foreach my $field (@fields) {
		die "Can't take reference to member $field of class $class. ".
			"Did you specify this field when calling $class->mk_refaccessors?"
			unless $CLASSES{$class}->{$field};
		push @refs, \$self->{$field};
	}
	return wantarray ? @refs : $refs[0];
}

=item B<import>

    use Class::Accessor::Ref qw(foo bar baz);

For the sake of convenience, you can specify what fields to generate
accessors for on the C<use> line. It also makes your calling package
a subclass of Class::Accessor::Ref, so you don't need to C<use base>.
If you want to generate refaccessors for only a subset of your regular
accessors, don't use this option, but rather make separate calls to
mk_accessors and mk_refaccessors. [Supporting this on the C<use> line
was considered, but I decided it was too cumbersome and would break code
that's just switching from Class::Accessor.]

    package Foo;
    use Class::Accessor::Ref qw(foo bar baz);

Is equivalent to

    package Foo;
    use Class::Accessor::Ref;
    use base 'Class::Accessor::Ref';
    Foo->mk_accessors(qw/foo bar baz/);
    Foo->mk_refaccessors(qw/foo bar baz/);

=back

=cut

sub import {
	my($class, @fields) = @_;
    return if !@fields;
	my $call_pkg = (caller)[0];
	if ($_DEBUG) { warn "$class: use C::A::R qw(".(join " ", @fields).")\n" }
    {
        # fake C<< packge Foo; use base 'Class::Accessor::Ref' >>
        no strict 'refs';
        push @{"$call_pkg\::ISA"}, $class;
    }
	if (@fields) {
		$call_pkg->mk_accessors(@fields);
		$call_pkg->mk_refaccessors(@fields);
	}
}

=head1 CAVEATS

Class::Accessor::Ref generates methods called _ref_SOMETHING in the
caller's namespace. Having an existing member whose name begins with
_ref_ would render the normal accessor to that member inaccessible,
so don't do that.

One point of Class::Accessor is to allow you to avoid changing members
directly. Since whoever gets hold of the return value of a _ref_ accessor
can circumvent any validations you may have imposed on the member (for
example, by overriding the normal setter method), this can be considered
somewhat unsafe. The main use of Class::Accessor::Ref is inside class
implementations, where you have control over who you trust with giving
a reference to your private data and who you don't.

=head1 COPYRIGHT (The "MIT" License)

Copyright 2003-2007 Gaal Yahas.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.


=head1 AUTHOR

Gaal Yahas <gaal@forum2.org>


=head1 SEE ALSO

L<Class::Accessor>

=cut



( run in 1.294 second using v1.01-cache-2.11-cpan-5511b514fd6 )